我创建了一个模块结构来保存我不想存储在数据库中的静态数据。像这样:
module Vixen
module Cateories
module Amazon
module THREE_D_PRINTED_PRODUCTS
REFERAL_FEE_PERCENTAGE = .12
MINIMUM_REFERAL_FEE = 0
NAME = '3D Printed Products'
end
module AMAZON_DEVICE_ACCESSORIES
REFERAL_FEE_PERCENTAGE = .25
MINIMUM_REFERAL_FEE = 0
NAME = 'Amazon Device Accessories'
end
module AMAZON_KINDLE
REFERAL_FEE_PERCENTAGE = .15
MINIMUM_REFERAL_FEE = 0
NAME = 'Amazon Kindle'
end
module AUTOMOTIVE_AND_POWERSPORTS
REFERAL_FEE_PERCENTAGE = .12
MINIMUM_REFERAL_FEE = 1.00
NAME = 'Amazon Kindle'
end
end
end
end
我正在尝试选择模块下的元素。是否可以循环每个模块:
Vixen::Cateories::Amazon.each { | category | print category::NAME }
我知道这段代码不能正常工作。
答案 0 :(得分:2)
我认为这可能就是你要做的事情:
Vixen::Cateories::Amazon.constants.each do
|category| print Vixen::Cateories::Amazon.const_get("#{category}::NAME")
end