Ruby | Enumerable partition() function
Last Updated :
05 Dec, 2019
The partition() of enumerable is an inbuilt method in Ruby returns two arrays, one containing the elements of the enumerable which return true, while the other contains the elements which returns false. It returns an enumerator if no block is passed.
Syntax enu.partition { |obj| block }
Parameters: The function takes a block according to which partition is to be done.
Return Value: It returns two arrays.
Example #1:
enu1 = [ 10 , 19 , 18 ]
enu1.partition { |num| num> 12 }
|
Output:
[[19, 18], [10]]
Example #2:
enu1 = ( 1 .. 100 )
enu1.partition
|
Output:
Enumerator: 1..100:partition
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...