Newb: an explaination for this if statement..

D

Dominic Son

Hi. I'm confused as to why this variable* is nessesary:

-----------

def add_product(product)

existing_product = @items.find {|item| item.product == product}

if existing_product
existing_product.increment_quantity

else
existing_product = CartItem.new(product)
@items << existing_product

end

existing_product*

end

--------------

i'm a newb, and to a newb, i can't understand why a var just sits there.
can someone please explain what the purpose of exisiting_product* is?

i'd figure exisiting_product* wouldn't be needed since on the 2nd line,
existing_product is initialized with @items.find..blahblahblah..
 
D

dblack

Hi --

Hi. I'm confused as to why this variable* is nessesary:

-----------

def add_product(product)

existing_product = @items.find {|item| item.product == product}

if existing_product
existing_product.increment_quantity

else
existing_product = CartItem.new(product)
@items << existing_product

end

existing_product*

end

--------------

i'm a newb, and to a newb, i can't understand why a var just sits there.
can someone please explain what the purpose of exisiting_product* is?

i'd figure exisiting_product* wouldn't be needed since on the 2nd line,
existing_product is initialized with @items.find..blahblahblah..

The last line is equivalent to:

return existing_product

If there's no explicit "return" statement, then the method returns the
value of the last expression evaluated -- which, in this case, is:

existing_product


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black => RUBY FOR RAILS, the Ruby book for
Rails developers
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
(e-mail address removed) => me
 
J

Justin Collins

Dominic said:
Hi. I'm confused as to why this variable* is nessesary:

-----------

def add_product(product)

existing_product = @items.find {|item| item.product == product}

if existing_product
existing_product.increment_quantity

else
existing_product = CartItem.new(product)
@items << existing_product

end

existing_product*

end

--------------

i'm a newb, and to a newb, i can't understand why a var just sits there.
can someone please explain what the purpose of exisiting_product* is?

i'd figure exisiting_product* wouldn't be needed since on the 2nd line,
existing_product is initialized with @items.find..blahblahblah..
In Ruby, methods return the value of the last expression if there is no
explicit return. That's why it looks like existing_product is just
sitting there. It's the same as:

def add_product(product)

...stuff..

return existing_product

end

-Justin
 
J

Jeff Cohen

Dominic said:
Hi. I'm confused as to why this variable* is nessesary:

-----------

def add_product(product)
[snip]

existing_product*

end

In Ruby, the last expression executed becomes the return value of the
method.

The code could have said:

return existing_product

but the "return" keyword is actually optional in this case.

Jeff
softiesonrails.com
 
B

bbiker

Jeff said:
Dominic said:
Hi. I'm confused as to why this variable* is nessesary:

-----------

def add_product(product)
[snip]

existing_product*

end

In Ruby, the last expression executed becomes the return value of the
method.

The code could have said:

return existing_product

but the "return" keyword is actually optional in this case.

Jeff
softiesonrails.com

So why not just use "return existing_product"
it definetily states its intent
also implies that the return value should be caught.

lots of methods can return values that do not need to be caught such as
puts which always returns nil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top