Open In App

Ruby | Struct filter() function

Improve
Improve
Like Article
Like
Save
Share
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 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]

Last Updated : 18 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads