Skip to content
Related Articles

Related Articles

Improve Article

Ruby | Hash any?() function

  • Last Updated : 07 Jan, 2020

any?() is a Hash class method which checks for the presence of a pattern and passes each element of the collection to the given block.

Syntax: Hash.any?()

Parameter: Hash for testing

Return: true – if the block ever returns a value other than false or nil otherwise return false

Example #1:






# Ruby code for any?() method
  
# declaring Hash value
a = { "a" => 100, "b" => 200 }
  
# declaring Hash value
b = {"a" => 100}
  
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
  
# checking pattern
puts "pattern : #{a.any?()}\n\n"
  
puts "pattern : #{b.any?()}\n\n"

Output :

pattern : true

pattern : true

Example #2:




# Ruby code for any?() method
  
# declaring Hash value
c = {}
  
puts "pattern : #{c.any?}\n\n"

Output :

pattern : false

My Personal Notes arrow_drop_up
Recommended Articles
Page :