# Dont def need return

**URL:** <https://www.ruby-forum.com/t/dont-def-need-return/147086>\
**Category:** Ruby\
**Created:** [August 28, 2008, 11:35am UTC](https://www.ruby-forum.com/t/dont-def-need-return/147086 "2008-08-28T11:35:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 28, 2008, 11:35am UTC](https://www.ruby-forum.com/t/dont-def-need-return/147086/1 "2008-08-28T11:35:11Z")

</div>

Hi  
I was learing Ruby and have code like  
Class TestClass  
def getcolors  
arr=[“Blue”,“Yellow”]  
arr1=[“1”,“2”]  
end  
end  
obj=TestClass.new  
puts obj.getcolors

```
       And the result is 1 2 ..This looks some strange to me since

```

I have not notice in other languages And if arr1=[“1”,“2”] is absent  
then it puts Blue Yellow …So dont we need return .I would like to know  
what happens here actually  
And another inteesing thing is if I write  
return arr=[“Blue”,“Yellow”] the result i get is Blue Yellow  
If I give  
return arr=[“Blue”,“Yellow”]  
return arr1=[“1”,“2”] then also I get "Blue Yellow

Why?  
Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 28, 2008, 11:41am UTC](https://www.ruby-forum.com/t/dont-def-need-return/147086/2 "2008-08-28T11:41:09Z")

</div>

Hi,

Ruby implicitly returns the last value of a code block unless you  
explicitly  
use return. In the last case it works as expected in all other  
languages.

Kind regards

Nicolai

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 28, 2008, 11:44am UTC](https://www.ruby-forum.com/t/dont-def-need-return/147086/3 "2008-08-28T11:44:40Z")

</div>

On Thu, Aug 28, 2008 at 11:30 AM, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> And the result is 1 2 ..This looks some strange to me since
> 
> ```
> 
> I have not notice in other languages And if arr1=[“1”,“2”] is absent  
> then it puts Blue Yellow …So dont we need return .I would like to know  
> what happens here actually

What happens is that Ruby returns the result of the last expression.  
This is true for methods and also for blocks:

def a  
“this is the last expression”  
end

a() # =\> “this is the last expression”

Another example:

def a(bool)  
if bool  
“it was true !”  
else  
“it was false !”  
end

a(true) # =\> “it was true !”  
a(false) # =\> “it was false !”

So, the return keyword is not needed. Some people say that (ab)using  
this feature  
can make the code more confusing, specially if the method or block is  
complex  
with branches, etc, and that explicitly using “return” is better.  
For simple methods is usually good enough, though.

> And another inteesing thing is if I write  
> return arr=[“Blue”,“Yellow”] the result i get is Blue Yellow  
> If I give  
> return arr=[“Blue”,“Yellow”]  
> return arr1=[“1”,“2”] then also I get "Blue Yellow  
> Why?

This is because “return” exits from the method, and so  
this line “return arr1=[“1”,“2”]” is never executed.

Jesus.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 28, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/dont-def-need-return/147086/4 "2008-08-28T12:21:02Z")

</div>

On Thu, Aug 28, 2008 at 11:30 AM, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

If you want to return both arrays combine them in another array on the  
last line: [arr, arr1] and you’ll get an array containing [[“Blue”,  
“Yellow”], [“1”, “2”]]

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 28, 2008, 11:55am UTC](https://www.ruby-forum.com/t/dont-def-need-return/147086/5 "2008-08-28T11:55:34Z")

</div>

Hi  
Thanks fr the reply  
Sijo
