Open In App

Ruby | Struct filter() function

Last Updated : 18 Dec, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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
# 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
# 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]

Next Article

Similar Reads