将数组变成哈希,然后添加键=>值,并在键存在时递增

时间:2019-07-02 13:18:51

标签: arrays ruby if-statement hash each

再试一次后,我认为这将需要一个带有do..end的方法。在下面,您将看到整个测试规范,以详细了解“购物车”的含义,到目前为止,我已经将我的代码和错误一并包括在内。

购物车以一系列单独的物品开始。使用consolidate_cart方法将其转换为包含每个项目计数的哈希。

  formReset(){
    this.focus();
  }

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

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

我真的可以使用解释/帮助。我也无法真正地单独研究每首颂歌。

2 个答案:

答案 0 :(得分:0)

def consolidate_cart(购物车)   #购物车是散列数组   newCart = {}

cart.each做| k |

void check_address(OpaqueObject* oo)
{
    Mystruct *ms = (Mystruct*)(*oo);
    ms->check_address();
}

结束   新车 结束

答案 1 :(得分:0)

我知道这有点老了,但我希望这可以帮助其他人。

看起来您和我正在从事同一件事。这是我的解决方案,它通过了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?因为它返回一个布尔值,所以if / else语句非常简单。 .include?方法在键中搜索匹配项。如果不匹配,则将整个哈希发送到new_cart。如果存在匹配项,则将:count增加1。您将需要使用循环,以便可以捕获起始数组内的所有元素。