# Simple Question

**URL:** <https://www.ruby-forum.com/t/simple-question/133005>\
**Category:** Ruby\
**Created:** [March 26, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/simple-question/133005 "2008-03-26T17:20:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 26, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/simple-question/133005/1 "2008-03-26T17:20:52Z")

</div>

Hi-

This should be an easy one to answer…

Is it possible to return an array from a function?

Example (although a very bad one):

def test(val1,val2)  
myarr = Array.new  
myarr = [val1,val2]  
return myarr  
end

begin  
thisarr = test(val1,val2)  
end

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 26, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/simple-question/133005/2 "2008-03-26T17:27:04Z")

</div>

On Mar 26, 10:17 am, pete [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> myarr = [val1,val2]  
> return myarr  
> end
> 
> begin  
> thisarr = test(val1,val2)  
> end

You have the code. Why not run it? It will answer your question.

Chris

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 26, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/simple-question/133005/3 "2008-03-26T17:28:20Z")

</div>

Hi –

On Thu, 27 Mar 2008, pete wrote:

> Hi-
> 
> This should be an easy one to answer…
> 
> Is it possible to return an array from a function?

Yes. You can return any object.

irb is really good for answering questions like this:

irb(main):001:0\> def return\_array  
irb(main):002:1\> [1,2,3]  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> return\_array  
=\> [1, 2, 3]

> def test(val1,val2)  
> myarr = Array.new  
> myarr = [val1,val2]

You’re re-assigning to myarr, which means that the first value will be  
discarded (unless it has another reference somewhere else (which it  
doesn’t in this case).

> return myarr  
> end

You could rewrite the above as:

def test(val1, val2)  
[val1, val2]  
end

Just return what you want to return – no need to dance around it 🙂

David

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 26, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/simple-question/133005/4 "2008-03-26T17:33:38Z")

</div>

On Mar 26, 2008, at 10:19 AM, pete wrote:

> myarr = [val1,val2]  
> return myarr  
> end
> 
> begin  
> thisarr = test(val1,val2)  
> end

Yes. Even shorter:

def test(val1,val2)  
[val1,val2]  
end

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 26, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/simple-question/133005/5 "2008-03-26T17:31:17Z")

</div>

> Is it possible to …

yes but you don’t need a lot of that extra syntax

def test(val1,val2)  
[val1,val2]  
end

thisarr = test(1.2,“three”)  
=\> [1.2, “three”]

pete [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: Hi-

This should be an easy one to answer…

Is it possible to return an array from a function?

Example (although a very bad one):

def test(val1,val2)  
myarr = Array.new  
myarr = [val1,val2]  
return myarr  
end

begin  
thisarr = test(val1,val2)  
end

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 26, 2008, 5:34pm UTC](https://www.ruby-forum.com/t/simple-question/133005/6 "2008-03-26T17:34:52Z")

</div>

pete wrote:

> Hi-
> 
> This should be an easy one to answer…

Since you’ve written all that code, it would be even easier (and get you  
a faster response) just to run it, surely?

Yes, you can return an Array (or any object) from a method.

Gareth
