How to Filter Items of a Set based on a Condition in Ruby


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,

  1. First, we need to require the `set` library using the `require 'set'` statement. This library provides the `Set` class.
  2. We then create a set named `numbers` containing integers `1`, `2`, `3`, `4`, and `5` by using `Set.new([1, 2, 3, 4, 5])`.
  3. 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.
  4. 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.
  5. 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,

  1. First, include the `set` library in your program by using the `require 'set'` statement to access the `Set` class.
  2. Create a set named `words` containing strings `"apple"`, `"banana"`, `"cherry"`, `"date"`, and `"fig"` using `Set.new(['apple', 'banana', 'cherry', 'date', 'fig'])`.
  3. 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.
  4. 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.
  5. 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,

  1. First, ensure the `set` library is included in your program by using the `require 'set'` statement to access the `Set` class.
  2. Create a set named `symbols` containing symbols `:apple`, `:banana`, `:avocado`, `:date`, and `:fig` using `Set.new([:apple, :banana, :avocado, :date, :fig])`.
  3. 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'.
  4. 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.
  5. 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

  1. How to create an Empty Set in Ruby ?
  2. How to Get Length of a Set in Ruby ?
  3. How to create a Set of size N in Ruby ?
  4. How to create a Set of Numbers from 1 to N in Ruby ?
  5. How to create a Set of integers in Ruby ?
  6. How to create a Set of Strings in Ruby ?
  7. How to Access Items in a Set in Ruby ?
  8. How to get a Random Item in a Set in Ruby ?
  9. How to Iterate Over a Set in Ruby ?
  10. How to check if a Set is Empty in Ruby ?
  11. How to check if a Set is Not Empty in Ruby ?
  12. How to get Subset from a Set in Ruby ?
  13. How to check if a Specific Item is present in the Set in Ruby ?
  14. How to check if a Set contains all the items of Another Set in Ruby ?
  15. How to Sort Items of a Set in Ruby ?
  16. How to Copy a Set in Ruby ?
  17. How to add an Item to a Set in Ruby ?
  18. How to find Union of Two Sets in Ruby ?
  19. How to find Intersection of Two Sets in Ruby ?
  20. How to check if Two Sets are Equal in Ruby ?
  21. How to Convert a Set of Integers to a Set of Strings in Ruby ?
  22. How to Convert a Set of Strings to a Set of Integers in Ruby ?
  23. How to Convert a Set of Floats to a Set of Strings in Ruby ?
  24. How to Convert a Set of Strings to a Set of Floats in Ruby ?
  25. How to Filter Items of a Set based on a Condition in Ruby ?
  26. How to Remove Specific Item from a Set in Ruby ?
  27. How to Remove Items from Set based on a Condition in Ruby ?
  28. How to create a Set of Sets in Ruby ?