Ruby | Hash to_proc method

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 :



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

Hash a to_proc form : #

Hash b to_proc form : #

Hash c to_proc form : #

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

Hash a to_proc form : #

Hash b to_proc form : #

Hash c to_proc form : #




My Personal Notes arrow_drop_up

Recommended Posts:

    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.