Related Articles

Related Articles

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for partition method in Enumerable
    
# Initialize an enumerable
enu1 = [10, 19, 18]   
  
# Prints
enu1.partition { |num| num>12

chevron_right


Output:

[[19, 18], [10]]

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for partition method in Enumerable
    
# Initialize an enumerable
enu1 = (1..100)
  
# Prints
enu1.partition 
  
  

chevron_right


Output:

Enumerator: 1..100:partition
My Personal Notes arrow_drop_up
Recommended Articles
Page :