【发布时间】:2011-12-21 09:47:07
【问题描述】:
我有以下 Ruby 模块:
module Test
Constant1 = {
:key1 => :value1,
:key2 => :value2
}
Constant2 = {
:key1 => :value1,
:key2 => :value2
}
end
我正在尝试遍历声明的 Hash 常量以打印使用以下代码定义的所有键:
Test.constants.each do |constant|
constant.keys.each do |key|
puts "key: #{key}"
end
end
但我得到一个NoMethodError: undefined method 'keys' for "Constant2":String,但我不知道如何将String 转换为真正的成本。有人知道怎么做吗?
【问题讨论】: