Ruby等效的hash.key访问就像在Javascript中一样?

时间:2011-09-09 22:26:14

标签: ruby hash

在Javascript中,可以使用裸字直接访问属性:

var evil = {"knievel":"evel","daredevil":"fearless"}
evil["knievel"] // "evel"
evil.knievel    // "evel"

Ruby中是否有类似的东西而不必evil["knievel"]

1 个答案:

答案 0 :(得分:2)

require "ostruct"

evil = OpenStruct.new(:knievel => "evel",:daredevil => "fearless")
puts evil.knievel