Hash#to_h()是一种Hash类方法,它返回哈希的自哈希表示
用法:Hash.to_h()
参数:散列值
返回:自我对象
示例1:
# Ruby code for Hash.to_h() 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_h Value
puts "Hash a to_h form : #{a.to_h()}\n\n"
puts "Hash b to_h form : #{b.to_h()}\n\n"
puts "Hash c to_h form : #{c.to_h()}\n\n"
输出:
Hash a to_h form : {:a=>100, :b=>200} Hash b to_h form : {:a=>100, :c=>300, :b=>200} Hash c to_h form : {:a=>100}
示例2:
# Ruby code for Hash.to_h() 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_h Value
puts "Hash a to_h form : #{a.to_h()}\n\n"
puts "Hash b to_h form : #{b.to_h()}\n\n"
puts "Hash c to_h form : #{c.to_h()}\n\n"
输出:
Hash a to_h form : {"a"=>100, "b"=>200} Hash b to_h form : {"a"=>100} Hash c to_h form : {"a"=>100, "c"=>300, "b"=>200}
相关用法
- Ruby Hash value?用法及代码示例
- Ruby Hash to_a用法及代码示例
- Ruby Hash store()用法及代码示例
- Ruby Hash select()用法及代码示例
- Ruby Hash select!()用法及代码示例
- Ruby String hash用法及代码示例
- Ruby Hash to_s用法及代码示例
- Ruby Hash replace()用法及代码示例
- Ruby Hash to_proc用法及代码示例
- Ruby Hash reject用法及代码示例
- Ruby Hash length()用法及代码示例
注:本文由纯净天空筛选整理自Kirti_Mangal大神的英文原创作品 Ruby | Hash to_h method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。