Agile Web Dev (book) question: errors

I had this same problem and I find this really weird because I was using
the actual code supplied by the book and was getting the error. When I
added an extra "end" to the bottom of the file it worked. Can someone
tell me why this code works:

class Cart
  attr_reader :items

  def initialize
      @items = []
  end

  def add_product(product)
    current_item = @items.find {|item| item.product == product}
    if current_item
      current_item.increment_quantity
    else
      @items << CartItem.new(product)
  end
end
end

It seems to me that there is one too many "end"s here.

Is that right? I am also a newb.

Thanksz!

class Cart
attr_reader :items

def initialize
@items = []
end

def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity
else
@items << CartItem.new(product)
end
end
end

It seems to me that there is one too many "end"s here.

Looks okay to me. The first "end" closes the "if-else" block
The second "end" closes the "def" method
The third "end" closes the "class" declaration

HI,
I have almost the same issue when I follow the 3rd Edtion.
but I verified the every type characters, there should be no mistakes.
Any one could help?

Are you absolutely certain that is the right cart.rb? Is that the
complete file? It looks ok to me.

Colin

clanlaw wrote:

I copied and pasted and ran this code and it worked fine for me. Any
stray "," characters?

There is something odd going on. I suggest you comment out the
contents of the file and add bits back in till it fails. Maybe you
have an unprintable character that is causing the problem.

Colin

Colin Law wrote: