如何将一个键的值作为操作的一部分用于Ruby中相同哈希中另一个键的值,在声明它的那一刻?
我的意思是这样的:
purchase = {product: 'phone',
quantity: 5,
price: 120,
total: self.quantity * self.price
}
我认为这会很有用,例如,如果值quantity
由外部API分配,其中消耗有限的查询,那么如果total
请求,则会花费两个查询而不是询问为数量'只花一个查询。
答案 0 :(得分:4)
我建议创建一个进行计算的Purchase
模型:
class Purchase
attr_reader :product, quantity, price
def initialize(attributes = {})
@product = attributes[:product]
@quantity = attributes[:quantity]
@price = attributes[:price]
end
def total
quantity * price
end
def to_h
{
product: product,
quantity: quantity,
price: price,
total: total
}
end
end
将代码更改为:
purchase = Purchase.new(product: 'phone', quantity: 5, price: 120).to_h
作为奖励:此模型易于理解且易于测试。
答案 1 :(得分:1)
在哈希宣言期间,您无法提出要求。您可以像这样更改代码:
purchase = {}
purchase[:product] = 'phone'
purchase[:quantity] = 5
purchase[:price] = 120
purchase[:total] = purchase[:quantity] * purchase[:price]
或者只是最后一行:
purchase = {
product: 'phone',
quantity: 5,
price: 120
}
purchase[:total] = purchase[:quantity] * purchase[:price]
或其他涉及稍微减少打字的愚蠢方式:
purchase = {
product: 'phone',
quantity: 5,
price: 120
}.tap{ |h| h[:total] = h[:quantity] * h[:price] }
但是,我建议在这种情况下你不应该在你的哈希中存储"denormalized" data。由于total
取决于数量或价格,如果其中任何一个更改,您的哈希将无效。
您可以通过在哈希上创建一个特殊的default_proc
来解决这个问题,每次请求时都会计算总数:
purchase = {
product: 'phone',
quantity: 5,
price: 120
}
purchase.default_proc = ->(h,k){
h[:quantity]*h[:price] if k==:total
}
p purchase[:total] #=> 600
purchase[:quantity] = 7
p purchase[:total] #=> 840
但是,创建一个类或Struct来更清楚。结构代码的代码较少:
Purchase = Struct.new(:product,:quantity,:price) do
def total
quantity * price
end
end
purchase = Purchase.new('phone',5,120)
p purchase.total #=> 600
purchase.quantity = 3
p purchase.total #=> 360
但是,Struct不会(by default)允许关键字参数。通过编写自己的类,您可以按任何顺序提供参数,甚至提供默认值:
class Purchase
attr_reader :product, :price, :quantity
def initialize(product:,price:,quantity:1) # default quantity
@product, @price, @quantity = product, price, quantity
end
def total
price*quantity
end
end
Purchase.new(price:10, product:'shoes').total #=> 10
Purchase.new(product:'shoes', price:10).total #=> 10
Purchase.new(quantity:3, price:10, product:'shoes').total #=> 30