Ruby | Struct filter() function
Last Updated :
12 Jul, 2025
Improve
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.
Ruby
Output:
Ruby
Output:
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? }
[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? }
[13]