[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Simple Question

pete

3/26/2008 4:18:00 PM

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
5 Answers

Chris Shea

3/26/2008 4:23:00 PM

0

On Mar 26, 10:17 am, pete <peterbattag...@gmail.com> 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

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

Chris

David A. Black

3/26/2008 4:25:00 PM

0

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

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.r... for details and updates!

Dan Fitzpatrick

3/26/2008 4:29:00 PM

0


On Mar 26, 2008, at 10:19 AM, pete 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
>

Yes. Even shorter:

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







Gareth Adams

3/26/2008 4:30:00 PM

0

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


Ron Fox

3/27/2008 9:13:00 AM

0

More precisely, ruby variables are references to objects, not the object
itself. You can return a reference to any type of object, including an
Array.

RF

Gareth Adams wrote:
> 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
>
>