将数组转换为散列,然后添加键 => 值并在存在键时递增

Turning an array into a hash, then, adding a key => value and increment when key is present

再次尝试后,我认为这需要一个带有 do..end 的 each 方法。您将在下面看到完整的测试规范,为您详细说明 'cart' 翻译成什么,我已经包含了我的代码以及错误。

购物车以单个项目的数组形式开始。使用 consolidate_cart 方法将其转换为包含每个项目计数的散列。

describe "Grocer" do
  let(:items) do
    [
      {"AVOCADO" => {:price => 3.00, :clearance => true}},
      {"KALE" => {:price => 3.00, :clearance => false}},
      {"BLACK_BEANS" => {:price => 2.50, :clearance => false}},
      {"ALMONDS" => {:price => 9.00, :clearance => false}},
      {"TEMPEH" => {:price => 3.00, :clearance => true}},
      {"CHEESE" => {:price => 6.50, :clearance => false}},
      {"BEER" => {:price => 13.00, :clearance => false}},
      {"PEANUTBUTTER" => {:price => 3.00, :clearance => true}},
      {"BEETS" => {:price => 2.50, :clearance => false}},
      {"SOY MILK" => {:price => 4.50, :clearance => true}}
    ]
  end

  let(:coupons) do
    [
      {:item => "AVOCADO", :num => 2, :cost => 5.00},
      {:item => "BEER", :num => 2, :cost => 20.00},
      {:item => "CHEESE", :num => 3, :cost => 15.00}
    ]
  end

  describe "#consolidate_cart" do
    it "adds a count of one to each item when there are no duplicates" do
      cart = [find_item('TEMPEH'), find_item('PEANUTBUTTER'), find_item('ALMONDS')]
      result = consolidate_cart(cart)
      result.each do |item, attributes|
        expect(attributes.keys).to include(:count)
        expect(attributes[:count]).to eq(1)
      end
    end

    it "increments count when there are multiple items" do
      avocado = find_item('AVOCADO')
      cart = [avocado, avocado, find_item('KALE')]

      result = consolidate_cart(cart)
      expect(result["AVOCADO"][:price]).to eq(3.00)
      expect(result["AVOCADO"][:clearance]).to eq(true)
      expect(result["AVOCADO"][:count]).to eq(2)

      expect(result["KALE"][:price]).to eq(3.00)
      expect(result["KALE"][:clearance]).to eq(false)
      expect(result["KALE"][:count]).to eq(1)
    end
  end

这是我到目前为止所做的:

def consolidate_cart(cart)

  newCart = {}

  cart.each do |item| 

    if cart[item.keys[0]]
        cart[item.keys[0]][:count] += 1;
    else
      cart[item[0]]
      #need to use values method to get the price and clearence added.
      binding.pry
    end
    newCart[cart]
  end
end

我收到以下错误:

  1) Grocer #consolidate_cart adds a count of one to each item when there are no duplicates
     Failure/Error: result = consolidate_cart(cart)

     TypeError:
       no implicit conversion of String into Integer
     # ./grocer.rb:7:in `block in consolidate_cart'
     # ./grocer.rb:5:in `each'
     # ./grocer.rb:5:in `consolidate_cart'
     # ./spec/grocer_spec.rb:28:in `block (3 levels) in <top (required)>''

我真的可以使用 explanation/help。我无法让我的撬动工作,也无法真正单独地查看每一首颂歌。

def consolidate_cart(购物车) # cart 是一个哈希数组 新购物车 = {}

cart.each 做 |k|

if newCart[k.keys[0]]
  newCart[k.keys[0]][:count] += 1
else
  newCart[k.keys[0]] = k[k.keys[0]]
  newCart[k.keys[0]][:count] = 1
end

结束 新购物车 结束

我知道这有点旧,但我希望这对其他人有所帮助。

看来你和我在做同样的事情。这是我的解决方案,它通过了 rspec 测试。

def consolidate_cart(cart)
 new_cart = {}
 i = 0 
 while cart.length > i do 
   if new_cart.keys.include?(cart[i].keys[0]) == false 
     new_cart.merge!(cart[i])
     new_cart[cart[i].keys[0]][:count] = 1
     i += 1 
   else 
     new_cart[cart[i].keys[0]][:count] += 1
     i += 1 
   end
end
   p new_cart
end 

我决定使用 .include?因为它 returns 是一个布尔值,所以 if/else 语句非常简单。 .include?方法在键中搜索匹配项。如果不匹配,则将整个散列发送到 new_cart。如果匹配,则将 :count 加 1。您需要使用循环,以便捕获起始数组内的所有元素。