1
我有这个类:转换哈希PARAMS到实例变量上的Ruby初始化
class PriceChange
attr_accessor :distributor_id, :product_id, :value, :price_changed_at, :realm
def initialize(data = {})
@distributor_id = data[:distributor_id]
@product_id = data[:product_id]
@value = data[:value]
@price_changed_at = data[:price_changed_at]
@realm = data[:realm]
end
end
我想避免的方法体内的映射。 我想要一个透明和优雅的方式来设置实例属性值。 我知道我可以遍历数据键并使用类似define_method
的东西。我不想要这个。 我想以一种干净的方式做到这一点。
您的代码无疑是非常干净的,但正如我说:“我知道我可以遍历数据键并使用类似define_method的东西“。我正在寻找一种“透明”的方式。也许不存在。 – Leantraxxx
@Leantraxxx它不存在,AFAICT –