我有这堂课:
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
. 我不想要这个。我想以一种干净的方式做到这一点。