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:
Num = Struct. new ( :a , :b , :c , :d )
l = Num. new ( 12 , 22 , 13 , 44 )
l.select {|v| v.even? }
|
Output:
[12, 22, 44]
Example 2:
Num = Struct. new ( :a , :b , :c , :d )
l = Num. new ( 12 , 22 , 13 , 44 )
l.select {|v| v.odd? }
|
Output:
[13]
Last Updated :
18 Dec, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...