Ruby 如何访问散列的值?

Ruby 如何访问散列的值?,ruby,hash,symbols,Ruby,Hash,Symbols,如何检索我正在查看的特定符号的值 如果我之前在散列中定义了一个符号 :red => "blue" 我可以在:red上调用什么方法来获得“blue”。若要使用\u和.id2name给我“红色”请使用: 你可以用 Ruby的文档非常好,访问键和值在的简介中有介绍。谢谢,因为我试图在散列之外定义的数组中访问它,所以我忽略了简单的答案作为解决方案。 >> h = {:red => "blue"} => {:red=>"blue"} >> h[:red]

如何检索我正在查看的特定符号的值

如果我之前在散列中定义了一个符号

:red => "blue"
我可以在:red上调用什么方法来获得“blue”。若要使用\u和.id2name给我“红色”

请使用:

你可以用


Ruby的文档非常好,访问键和值在的简介中有介绍。谢谢,因为我试图在散列之外定义的数组中访问它,所以我忽略了简单的答案作为解决方案。
>> h = {:red => "blue"}
=> {:red=>"blue"}
>> h[:red]
=> "blue"
h = {:red => "blue"}
h.fetch(:red) # => "blue"