[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newb: an explaination for this if statement..

Dominic Son

7/12/2006 7:30:00 PM

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..


--
Posted via http://www.ruby-....

7 Answers

dblack

7/12/2006 7:32:00 PM

0

Justin Collins

7/12/2006 7:34:00 PM

0

Dominic Son wrote:
> 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

Dominic Son

7/12/2006 7:38:00 PM

0

Hey Mr David. I really appriciate you helping me.

Thank you : )

Dominic

unknown wrote:
> Hi --
>
> On Thu, 13 Jul 2006, Dominic Son wrote:
>
>>
>> --------------
>>
>> 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


--
Posted via http://www.ruby-....

Jeff

7/12/2006 7:40:00 PM

0

Dominic Son wrote:
> 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

--
Posted via http://www.ruby-....

Dominic Son

7/12/2006 7:41:00 PM

0

I would've thanked you too Justin! we posted literally at the same time!

--
Posted via http://www.ruby-....

Pierre Barbier de Reuille

7/12/2006 7:41:00 PM

0

dblack@wobblini.net wrote:
> Hi --
>
> On Thu, 13 Jul 2006, Dominic Son wrote:
>
>> 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
>
Well, I don't know how other feels, but I'd put it in another way: In
Ruby the value of a sequence of expressions is the last expression. Now,
the return value of a function is the value of its content ... thus the
value of its last expression. "return" is, in ruby, only useful if you
want to return from the function before the end of the body of the
function (like break in an iterator will interrupt the iteration
returning the value after the break).

Pierre


Bernard Kenik

7/15/2006 4:30:00 PM

0


Jeff Cohen wrote:
> Dominic Son wrote:
> > 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