How to Filter Items of a Set based on a Condition in Ruby - Step by Step Examples
How to Filter Items of a Set based on a Condition in Ruby ?
Answer
To filter items of a Set based on a condition in Ruby, you can use the `select` method provided by the `Set` class from the `set` library.
✐ Examples
1 Filter Even Numbers from Set
In this example,
- First, we need to require the `set` library using the `require 'set'` statement. This library provides the `Set` class.
- We then create a set named `numbers` containing integers `1`, `2`, `3`, `4`, and `5` by using `Set.new([1, 2, 3, 4, 5])`.
- Next, we use the `select` method on the `numbers` set to filter out the even numbers. The statement `even_numbers = numbers.select { |num| num.even? }` iterates through each element in the set and selects only those elements that satisfy the condition `num.even?`, which checks if a number is even.
- The result of the `select` method is assigned to the variable `even_numbers`, which is a new set containing only the even numbers from the original set.
- Finally, we print the `even_numbers` set using the `puts` statement to see the filtered set.
Ruby Program
require 'set'
# Creating a set with integers
numbers = Set.new([1, 2, 3, 4, 5])
# Filtering even numbers from the set
even_numbers = numbers.select { |num| num.even? }
# Printing the set after filtering
puts "Even numbers in the set: #{even_numbers.to_a}"
Output
Even numbers in the set: [2, 4]
2 Filter Strings Longer than 5 Characters from Set
In this example,
- First, include the `set` library in your program by using the `require 'set'` statement to access the `Set` class.
- Create a set named `words` containing strings `"apple"`, `"banana"`, `"cherry"`, `"date"`, and `"fig"` using `Set.new(['apple', 'banana', 'cherry', 'date', 'fig'])`.
- Use the `select` method on the `words` set to filter out the strings that are longer than 5 characters. The statement `long_words = words.select { |word| word.length > 5 }` iterates through each element in the set and selects only those elements that satisfy the condition `word.length > 5`, which checks if a string's length is greater than 5.
- The result of the `select` method is assigned to the variable `long_words`, which is a new set containing only the strings longer than 5 characters from the original set.
- Print the `long_words` set using the `puts` statement to display the filtered set.
Ruby Program
require 'set'
# Creating a set with strings
words = Set.new(['apple', 'banana', 'cherry', 'date', 'fig'])
# Filtering strings longer than 5 characters from the set
long_words = words.select { |word| word.length > 5 }
# Printing the set after filtering
puts "Words longer than 5 characters in the set: #{long_words.to_a}"
Output
Words longer than 5 characters in the set: ["banana", "cherry"]
3 Filter Symbols Starting with 'a' from Set
In this example,
- First, ensure the `set` library is included in your program by using the `require 'set'` statement to access the `Set` class.
- Create a set named `symbols` containing symbols `:apple`, `:banana`, `:avocado`, `:date`, and `:fig` using `Set.new([:apple, :banana, :avocado, :date, :fig])`.
- Use the `select` method on the `symbols` set to filter out the symbols that start with the letter 'a'. The statement `a_symbols = symbols.select { |sym| sym.to_s.start_with?('a') }` iterates through each element in the set and selects only those elements that satisfy the condition `sym.to_s.start_with?('a')`, which checks if the symbol's string representation starts with 'a'.
- The result of the `select` method is assigned to the variable `a_symbols`, which is a new set containing only the symbols that start with 'a' from the original set.
- Print the `a_symbols` set using the `puts` statement to display the filtered set.
Ruby Program
require 'set'
# Creating a set with symbols
symbols = Set.new([:apple, :banana, :avocado, :date, :fig])
# Filtering symbols starting with 'a' from the set
a_symbols = symbols.select { |sym| sym.to_s.start_with?('a') }
# Printing the set after filtering
puts "Symbols starting with 'a' in the set: #{a_symbols.to_a}"
Output
Symbols starting with 'a' in the set: [:apple, :avocado]
Summary
In this tutorial, we learned How to Filter Items of a Set based on a Condition in Ruby language with well detailed examples.
More Ruby Sets Tutorials
- How to create an Empty Set in Ruby ?
- How to Get Length of a Set in Ruby ?
- How to create a Set of size N in Ruby ?
- How to create a Set of Numbers from 1 to N in Ruby ?
- How to create a Set of integers in Ruby ?
- How to create a Set of Strings in Ruby ?
- How to Access Items in a Set in Ruby ?
- How to get a Random Item in a Set in Ruby ?
- How to Iterate Over a Set in Ruby ?
- How to check if a Set is Empty in Ruby ?
- How to check if a Set is Not Empty in Ruby ?
- How to get Subset from a Set in Ruby ?
- How to check if a Specific Item is present in the Set in Ruby ?
- How to check if a Set contains all the items of Another Set in Ruby ?
- How to Sort Items of a Set in Ruby ?
- How to Copy a Set in Ruby ?
- How to add an Item to a Set in Ruby ?
- How to find Union of Two Sets in Ruby ?
- How to find Intersection of Two Sets in Ruby ?
- How to check if Two Sets are Equal in Ruby ?
- How to Convert a Set of Integers to a Set of Strings in Ruby ?
- How to Convert a Set of Strings to a Set of Integers in Ruby ?
- How to Convert a Set of Floats to a Set of Strings in Ruby ?
- How to Convert a Set of Strings to a Set of Floats in Ruby ?
- How to Filter Items of a Set based on a Condition in Ruby ?
- How to Remove Specific Item from a Set in Ruby ?
- How to Remove Items from Set based on a Condition in Ruby ?
- How to create a Set of Sets in Ruby ?