# Newb: an explaination for this if statement

**URL:** <https://www.ruby-forum.com/t/newb-an-explaination-for-this-if-statement/98161>\
**Category:** Ruby\
**Created:** [July 12, 2006, 9:29pm UTC](https://www.ruby-forum.com/t/newb-an-explaination-for-this-if-statement/98161 "2006-07-12T21:29:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [July 12, 2006, 9:29pm UTC](https://www.ruby-forum.com/t/newb-an-explaination-for-this-if-statement/98161/1 "2006-07-12T21:29:28Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [July 12, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/newb-an-explaination-for-this-if-statement/98161/2 "2006-07-12T21:33:12Z")

</div>

Hi –

On Thu, 13 Jul 2006, Dominic S. 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

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [July 12, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/newb-an-explaination-for-this-if-statement/98161/3 "2006-07-12T21:36:46Z")

</div>

Dominic S. 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…

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

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [July 12, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/newb-an-explaination-for-this-if-statement/98161/4 "2006-07-12T21:37:16Z")

</div>

Hey Mr David. I really appriciate you helping me.

Thank you : )

Dominic

unknown wrote:

> Hi –
> 
> On Thu, 13 Jul 2006, Dominic S. 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

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [July 12, 2006, 9:40pm UTC](https://www.ruby-forum.com/t/newb-an-explaination-for-this-if-statement/98161/5 "2006-07-12T21:40:10Z")

</div>

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

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [July 12, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/newb-an-explaination-for-this-if-statement/98161/6 "2006-07-12T21:46:41Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > existing\_product = @items.find {|item| item.product == product}  
> > 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

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [July 15, 2006, 6:35pm UTC](https://www.ruby-forum.com/t/newb-an-explaination-for-this-if-statement/98161/7 "2006-07-15T18:35:44Z")

</div>

Jeff C. wrote:

> > 
> 
> Jeff  
> [softiesonrails.com](http://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

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [July 12, 2006, 9:40pm UTC](https://www.ruby-forum.com/t/newb-an-explaination-for-this-if-statement/98161/8 "2006-07-12T21:40:22Z")

</div>

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