Ruby | Enumerable take_while() function
The take_while() of enumerable is an inbuilt method in Ruby returns all the elements till the first element which returns a false to the condition in the block. It returns an enumerator if the block is not passed.
Syntax: enu.take_while {|obj| block|}
Parameters: The function accepts a block.
Return Value: It returns elements till the first element which returns a false.
Example 1:
# Ruby program for take_while method in Enumerable # Initialize enu = [10, 12, 11, 15] # Prints enu.take_while {|obj| obj<=13} |
Output:
[10, 12, 11]
Example 2:
# Ruby program for take_while method in Enumerable # Initialize enu = (1..8) # Prints enu.take_while {|obj| obj<=4} |
Output:
[1, 2, 3, 4]
Contact Us