红宝石 |哈希 to_proc 方法
Hash#to_proc() : to_proc()是一个 Hash 类方法,它返回一个 Proc,它将“keys”值映射到“value”值。
Syntax: Hash.to_proc()
Parameter: Hash values
Return: Proc which maps ‘keys’ value to ‘value’ value.
示例 #1:
# 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"
输出 :
Hash a to_proc form : #
Hash b to_proc form : #
Hash c to_proc form : #
示例 #2:
# 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"
输出 :
Hash a to_proc form : #
Hash b to_proc form : #
Hash c to_proc form : #