Skip to content
Related Articles

Related Articles

Ruby | Hash include?() function
  • Last Updated : 07 Jan, 2020

Hash#include?() is a Hash class method which checks whether the given key is present in hash.

Syntax: Hash.include?()

Parameter: Hash values

Return: true – if given key”” is present in hash otherwise return false

Example #1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for Hash.has_value?() method
  
# declaring Hash value
a = {a:100, b:200}
  
# declaring Hash value
b = {a:100, c:300, b:200}
  
# declaring Hash value
c = {a:100}
  
  
# has_value? Value
puts "Hash a has_value? form : #{a.include?("a")}\n\n"
  
puts "Hash b has_value? form : #{b.has_value?("c")}\n\n"
  
puts "Hash c has_value? form : #{c.has_value?("c")}\n\n"

chevron_right


Output :

Hash a has_value? form : false

Hash b has_value? form : false

Hash c has_value? form : false

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for Hash.has_value?() 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}
  
  
# has_value? Value
puts "Hash a has_value? form : #{a.include?("a")}\n\n"
  
puts "Hash b has_value? form : #{b.has_value?("c")}\n\n"
  
puts "Hash c has_value? form : #{c.has_value?("c")}\n\n"

chevron_right


Output :

Hash a has_value? form : true

Hash b has_value? form : false

Hash c has_value? form : false

My Personal Notes arrow_drop_up
Recommended Articles
Page :