gpt4 book ai didi

Ruby Select 方法(用于数组)问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:11:45 27 4
gpt4 key购买 nike

我正在运行以下方法,并且成功地将两个参数(库存、数量)传递给该方法。但是我错误地使用了 .first.each 方法。我正在尝试将 .each 替换为 .select 以选择具有 Inventory id: 6

的购物车商品

可能的 .each 替换:(不起作用)inventory_to_increment = @items.select{|item| item_id == inventory}

def increment_inventory_quantity(inventory, quantity)
inventory_to_increment = @items.each{|item| item.inventory == inventory}
unless inventory_to_increment.empty?
inventory_to_increment = inventory_to_increment.first
else
# error handling here
end
inventory_to_increment.quantity = quantity.to_i
end

我使用 Ruby Debugger 来调试我的代码:

inventory_to_increment = @items.each{|item| item.inventory == 库存}

p inventory = 6

除非inventory_to_increment.empty?

CartItem:0x102c4a4c0 @quantity=22, @inventory=#<Inventory id: 1

CartItem:0x102c49638 @quantity=2, @inventory=#<Inventory id: 8

CartItem:0x102c48918 @quantity=4, @inventory=#<Inventory id: 50

CartItem:0x102c47b80 @quantity=2, @inventory=#<Inventory id: 6

inventory_to_increment.first

CartItem:0x102c4a4c0 @quantity=22, @inventory=#<Inventory id: 1 

inventory_to_increment.quantity = quantity.to_i

= 3

我尝试了几种组合,我需要一些基本的 Ruby 数组指导。提前致谢!

更新答案的输出

inventory_to_increment = @items.select{|item| item.inventory_id == inventory}
(rdb:1) list
[21, 30] in /Users/justin/cart/app/models/cart.rb
21
22
23 def increment_inventory_quantity(inventory, quantity)
24 debugger
25
=> 26 inventory_to_increment = @items.select{|item| item.inventory_id == inventory}
27 unless inventory_to_increment.empty?
28 inventory_to_increment.first
29 end
30
(rdb:1) p @items
[#<CartItem:0x102df1828 @quantity=22, @inventory=#<Inventory id: 1>>, #<CartItem:0x102df09a0 @quantity=2, @inventory=#<Inventory id: 8>>, #<CartItem:0x102ded908 @quantity=21, @inventory=#<Inventory id: 50>>]
(rdb:1) p inventory
50
(rdb:1) p quantity
"11"
(rdb:1) p item.inventory.id
NameError Exception: undefined local variable or method `item' for #<Cart:0x102df18f0>
(rdb:1) p item.inventory_id
NameError Exception: undefined local variable or method `item' for #<Cart:0x102df18f0>
(rdb:1) next
/Users/justin/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:162
rescue_action(exception)

最佳答案

Inventory 似乎是一个对象,如果是这样,您的 item.inventory == inventory 可能无法按预期工作。假设您有一个 Item 类。

class Item
attr_accessor :inventory_id, :quantity

def initialize(inventory_id, quantity)
@inventory_id = inventory_id
@quantity = quantity
end
end

然后您创建两个对象并比较它们:

>> item1=Item.new(8,2)
=> #<Item:0xb7b733bc @inventory_id=8, @quantity=2>
>> item2=Item.new(8,2)
=> #<Item:0xb7b6b7c0 @inventory_id=8, @quantity=2>
>> item1==item2
=> false

这是因为您将比较对象 ID(0xb7b733bc 到 0xb7b6b7c0)而不是对象的内容。

>> item1.inventory_id==item2.inventory_id
=> true

看来您只想将 数量 添加到与库存 ID 匹配的第一个项目。如果这是正确的,那么你可以尝试这样的事情

 inventory_to_increment=items.select {|item| item.inventory.inventory_id==inventory}
unless inventory_to_increment.empty?
inventory_to_increment.first.quantity+=quantity
end

关于Ruby Select 方法(用于数组)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2088374/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com