Open In App

Ruby | Hash to_proc method

Last Updated : 07 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
Hash#to_proc() : to_proc() is a Hash class method which returns a Proc which maps 'keys' value to 'value' value.
Syntax: Hash.to_proc() Parameter: Hash values Return: Proc which maps 'keys' value to 'value' value.
Example #1 : Ruby
# Ruby code for Hash.to_proc() 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}


# to_proc Value
puts "Hash a to_proc form : #{a.to_proc()}\n\n"

puts "Hash b to_proc form : #{b.to_proc()}\n\n"

puts "Hash c to_proc form : #{c.to_proc()}\n\n"
Output :
Hash a to_proc form : #

Hash b to_proc form : #

Hash c to_proc form : #

Example #2 : Ruby
# Ruby code for Hash.to_proc() 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}

# to_proc Value
puts "Hash a to_proc form : #{a.to_proc()}\n\n"

puts "Hash b to_proc form : #{b.to_proc()}\n\n"

puts "Hash c to_proc form : #{c.to_proc()}\n\n"
Output :
Hash a to_proc form : #

Hash b to_proc form : #

Hash c to_proc form : #


Similar Reads