Ruby | Struct filter() function
The filter() is an inbuilt method in Ruby that returns an array which contains the member value from struct which returns a true value for the given block.
Syntax: filter {|obj| block }
Parameters: The function accepts a single parameter block which specifies the condition.
Return Value: It returns member value from struct to block and an array is returned.
Example 1:
# Ruby program for filter method in struct # Initialize struct Num = Struct. new ( :a , :b , :c , :d ) # Initialize numbers l = Num. new ( 12 , 22 , 13 , 44 ) # Filter used l.select {|v| v.even? } |
Output:
[12, 22, 44]
Example 2:
# Ruby program for filter method in struct # Initialize struct Num = Struct. new ( :a , :b , :c , :d ) # Initialize numbers l = Num. new ( 12 , 22 , 13 , 44 ) # Filter used l.select {|v| v.odd? } |
Output:
[13]
Contact Us