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:
# Ruby program for partition method in Enumerable # Initialize an enumerable enu1 = [ 10 , 19 , 18 ]
# Prints enu1.partition { |num| num> 12 }
|
Output:
[[19, 18], [10]]
Example #2:
# Ruby program for partition method in Enumerable # Initialize an enumerable enu1 = ( 1 .. 100 )
# Prints enu1.partition |
Output:
Enumerator: 1..100:partition