# How to dump contents of an array as parameters

**URL:** <https://www.ruby-forum.com/t/how-to-dump-contents-of-an-array-as-parameters/112708>\
**Category:** Ruby\
**Created:** [August 22, 2007, 12:53pm UTC](https://www.ruby-forum.com/t/how-to-dump-contents-of-an-array-as-parameters/112708 "2007-08-22T12:53:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![akifusenet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@akifusenet](https://www.ruby-forum.com/u/akifusenet)\
**Post date:** [August 22, 2007, 12:53pm UTC](https://www.ruby-forum.com/t/how-to-dump-contents-of-an-array-as-parameters/112708/1 "2007-08-22T12:53:09Z")

</div>

Hi  
Ä± am a beginner Rubyist and I have a small problem.

Track=Struct.new(:name,:surname)

#This works perfectly  
tempTrack=Track.new(15,“b”)  
p tempTrack.name  
p tempTrack.surname

But I want to create a temptrack with an incoming array how can i  
convert

[15,“b”] in to 15,“b”

#And of course this doesnt work  
incomingArray=[15,“b”]  
tempTrack=Track.new(incomingArray)  
p tempTrack.name  
p tempTrack.surname

Thanks.  
Akif,

---

<div class="post-metadata">

**Author:** ![akifusenet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@akifusenet](https://www.ruby-forum.com/u/akifusenet)\
**Post date:** [August 22, 2007, 12:58pm UTC](https://www.ruby-forum.com/t/how-to-dump-contents-of-an-array-as-parameters/112708/2 "2007-08-22T12:58:41Z")

</div>

Alle mercoledÃ¬ 22 agosto 2007, Akif T. ha scritto:

> p tempTrack.name  
> p tempTrack.surname
> 
> Thanks.  
> Akif,

You need to do this:

Track.new(\*[15, ‘b’])

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![akifusenet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@akifusenet](https://www.ruby-forum.com/u/akifusenet)\
**Post date:** [August 22, 2007, 1:11pm UTC](https://www.ruby-forum.com/t/how-to-dump-contents-of-an-array-as-parameters/112708/3 "2007-08-22T13:11:27Z")

</div>

Stefano C. wrote:

> You need to do this:
> 
> Track.new(\*[15, ‘b’])
> 
> I hope this helps
> 
> Stefano

Thank you Stefano. It worked.

Akif,

---

<div class="post-metadata">

**Author:** ![akifusenet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@akifusenet](https://www.ruby-forum.com/u/akifusenet)\
**Post date:** [August 22, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/how-to-dump-contents-of-an-array-as-parameters/112708/4 "2007-08-22T15:01:16Z")

</div>

On Aug 22, 7:11 am, Akif T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Akif,
> 
> Posted viahttp://www.ruby-forum.com/.

Just to expand (pun?) the concept:

the \* operator, when applied to method arguments, work two ways, if I  
understand correctly:

In a method call, putting \* before an array breaks it apart into its  
elements and passes them as individual arguments to a method. So  
x.do\_stuff(a,b,c,)  
is equivalent to  
x.do\_stuff(\*[a,b,c])  
or  
y = [a,b,c]  
x.do\_stuff(\*y)

In a method declaration, \* can be used to collect an undetermined  
number of arguments into an array. Suppose that you have a method that  
accepts optional arguments, and depending on the options there could  
be 1, 2, or 3 values passed in. One way (there are others) to handle  
that would be to make the method look like this:

def do\_stuff(\*args)  
if args.length \> 2  
puts “optional stuff”  
else  
puts “default stuff”  
end  
end

I believe I’ve gotten this right, but I’m something of a Nuby myself.  
Any comments or corrections of my explanation would be greatly  
appreciated.

HTH,  
Andrew
