我刚开始在ruby中使用常量。
我有
module Constants
C1 = "foo"
C2 = "bar"
end
我想这样做
Constants.each do |c|
#do something with each one
end
但它说
undefined method ‘each’ for Constants::module
…
有没有一种迭代常量列表的好方法?
最佳答案
module Constants
C1 = "foo"
C2 = "bar"
end
Constants.constants.each do |c|
puts "#{c}: #{Constants.const_get(c)}"
end
#=> "C1: foo"
#=> "C2: bar"
关于ruby - Ruby:遍历常量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6712621/