Hash
1. include? , has_key?
It will return true if the object passed in match the KEY in the hash.
eg: a = {:A => 200 , :B => 300}
a.include?(200)
returns false
a.has_key?(:A)
return true
2. each { |key, value| }
It will iterate through the hash key once. Returns the original full hash.
eg: a = {:A => 200 , :B => 300}
a.each do |key, value|
puts key
puts value
end
# A
# 200
# B
# 300
# return {:A => 200 , :B => 300}
3. fetch
It will return the value based on the hash key inputted
eg: a = {:A => 200 , :B => 300}
a.fetch(:A)
# return 200
4. key
It will return the key based on the value passed in.
eg: a = {:A => 200 , :B => 300}
a.key(200)
# return :A
1. include? , has_key?
It will return true if the object passed in match the KEY in the hash.
eg: a = {:A => 200 , :B => 300}
a.include?(200)
returns false
a.has_key?(:A)
return true
2. each { |key, value| }
It will iterate through the hash key once. Returns the original full hash.
eg: a = {:A => 200 , :B => 300}
a.each do |key, value|
puts key
puts value
end
# A
# 200
# B
# 300
# return {:A => 200 , :B => 300}
3. fetch
It will return the value based on the hash key inputted
eg: a = {:A => 200 , :B => 300}
a.fetch(:A)
# return 200
4. key
It will return the key based on the value passed in.
eg: a = {:A => 200 , :B => 300}
a.key(200)
# return :A
Comments
Post a Comment