Ruby Hash inspect()函数
Hash#inspect() 是一个Hash类方法,它给出了哈希的字符串表示。
语法: Hash.inspect()
参数: 哈希值
返回: 哈希的字符串表示法。
例子 #1 :
# Ruby code for Hash.inspect() 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}
# inspect Value
puts "Hash a inspect form : #{a.inspect()}\n\n"
puts "Hash b inspect form : #{b.inspect()}\n\n"
puts "Hash c inspect form : #{c.inspect()}\n\n"
输出:
Hash a inspect form : {:a=>100, :b=>200}
Hash b inspect form : {:a=>100, :c=>300, :b=>200}
Hash c inspect form : {:a=>100}
例子 #2 :
# Ruby code for Hash.inspect() 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}
# inspect Value
puts "Hash a inspect form : #{a.inspect()}\n\n"
puts "Hash b inspect form : #{b.inspect()}\n\n"
puts "Hash c inspect form : #{c.inspect()}\n\n"
输出:
Hash a inspect form : {"a"=>100, "b"=>200}
Hash b inspect form : {"a"=>100}
Hash c inspect form : {"a"=>100, "c"=>300, "b"=>200}