Open In App

Ruby | Hash any?() function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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


Last Updated : 07 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads