1

I have a hash where each value is an array and am having trouble updating location 1 in the array as needed. My code is below.

newOrderItem = STDIN.gets.chomp

while newOrderItem != "q"
  if items.has_key?(newOrderItem) == true
    print "How many #{newOrderItem}'s would you like?"
    qty = STDIN.gets.chomp
    items[newOrderItem[1]] = items[newOrderItem[1]] + qty
    puts "Please enter the item you whish to order. Enter \"q\" when you have finished."
    newOrderItem = STDIN.gets.chomp
  else
    puts "Sorry, #{newOrderItem} is not on our menu. Please try again."
    newOrderItem = STDIN.gets.chomp
  end
end

The line where I think I am in trouble is "items[newOrderItem[1]] = items[newOrderItem[1]] + qty" When I run this program, I get an undefined method error on the plus sign. What I would like to do is have the qty added to the current value (always an int) in the array for that key. My hash looks like this.

items = { 'Hamburger' => [3.65, 0], 'Fries' => [1.00, 0], 'Drink' => [1.49, 0], 'Shake' => [2.25, 0], 'Chicken Nuggets' => [0.99, 0]  } 

Thanks for the help!

3
  • joerdie, it's perfectly OK for you to say, "I'm a Ruby newbie", but try to avoid having your code scream that out in the very first line. I'm speaking of the variable newOrderItem, which does not conform to Ruby's naming convention. :-) Commented Mar 28, 2014 at 20:56
  • Underscores instead of camel case for a local variable? Meh. And putting a smiley at the end of something does not make it come across as less condescending. Commented Mar 29, 2014 at 20:46
  • I did not mean it to be condescending. I just wanted you to know that 99.9% of the Ruby code you will read will conform to a particular style, that includes indentation (2 spaces), naming of variables and methods, and so on. If you want to deviate from that, that's your choice. Commented Mar 29, 2014 at 20:54

1 Answer 1

2

Right now you get an undefined method error, since items[newOrderItem[1]] returns nil. To access the hash you should use items[newOrderItem][1], which looks for the key newOrderItem, then finds the array and returns the element at index 1 of the array.

As well as this when you do the addition on line items[newOrderItem[1]] + qty qty is still a string.You must convert it to a numerical type first, use Integer(qty) if you want to convert it to a non-decimal value, or float(qty) if you want a decimal numerical value. So you would have something like this items[newOrderItem[1]] + Integer(qty).

Eg:

2.0.0-p353 :019 > items = { 'Hamburger' => [3.65, 0], 'Fries' => [1.00, 0], 'Drink' => [1.49, 0], 'Shake' => [2.25, 0], 'Chicken Nuggets' => [0.99, 0]  } 
 => {"Hamburger"=>[3.65, 0], "Fries"=>[1.0, 0], "Drink"=>[1.49, 0], "Shake"=>[2.25, 0], "Chicken Nuggets"=>[0.99, 0]} 
2.0.0-p353 :020 > items['Hamburger'][1]
 => 0 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This is my final line. items[newOrderItem][1] = items[newOrderItem][1] + Integer(qty) and it works perfectly. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.