# Function accepting argument list or array

**URL:** <https://www.ruby-forum.com/t/function-accepting-argument-list-or-array/187320>\
**Category:** Ruby\
**Created:** [April 21, 2010, 5:45pm UTC](https://www.ruby-forum.com/t/function-accepting-argument-list-or-array/187320 "2010-04-21T17:45:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 21, 2010, 5:45pm UTC](https://www.ruby-forum.com/t/function-accepting-argument-list-or-array/187320/1 "2010-04-21T17:45:28Z")

</div>

How would I write a function that treats fn(:a, :b) the same as it  
does fn([:a, :b])? The only way I can do this right now is by checking  
if the first argument is an array, but I thought there was a simpler  
way to do this, involving the splat operator ‘\*’.

Thomas

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 21, 2010, 5:53pm UTC](https://www.ruby-forum.com/t/function-accepting-argument-list-or-array/187320/2 "2010-04-21T17:53:05Z")

</div>

On Wed, Apr 21, 2010 at 5:45 PM, Thomas A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How would I write a function that treats fn(:a, :b) the same as it  
> does fn([:a, :b])? The only way I can do this right now is by checking  
> if the first argument is an array, but I thought there was a simpler  
> way to do this, involving the splat operator ‘\*’.

irb(main):013:0\> def method \*args  
irb(main):014:1\> p args  
irb(main):015:1\> end  
=\> nil  
irb(main):016:0\> method 1,2  
[1, 2]  
=\> nil  
irb(main):017:0\> method [1,2]  
[[1, 2]]  
=\> nil  
irb(main):018:0\> method \*[1,2]  
[1, 2]  
=\> nil

If you can change the call to using the splat when you have an array,  
then this works. If not you can do this:

irb(main):019:0\> def method \*args  
irb(main):020:1\> args = args.flatten  
irb(main):021:1\> p args  
irb(main):022:1\> end  
=\> nil  
irb(main):023:0\> method 1,2  
[1, 2]  
=\> nil  
irb(main):024:0\> method [1,2]  
[1, 2]

although be careful, this will flatten nested arrays all along (don’t  
know if this is good or bad for you):

irb(main):025:0\> method [[1,2],[3,4]]  
[1, 2, 3, 4]

Jesus.

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 21, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/function-accepting-argument-list-or-array/187320/3 "2010-04-21T18:00:42Z")

</div>

On Apr 21, 11:52 am, Jesús Gabriel y Galán [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> although be careful, this will flatten nested arrays all along (don’t  
> know if this is good or bad for you):

No, this works well for what I’m doing, thanks for the help.

Thomas

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 21, 2010, 6:20pm UTC](https://www.ruby-forum.com/t/function-accepting-argument-list-or-array/187320/4 "2010-04-21T18:20:35Z")

</div>

On 04/21/2010 05:52 PM, Jesús Gabriel y Galán wrote:

> although be careful, this will flatten nested arrays all along (don’t  
> know if this is good or bad for you):
> 
> irb(main):025:0\> method [[1,2],[3,4]]  
> [1, 2, 3, 4]

You can control that at least in 1.8.7 and 1.9.\*:

irb(main):001:0\> a=[[[1,2],3],4]  
=\> [[[1, 2], 3], 4]  
irb(main):002:0\> a.flatten  
=\> [1, 2, 3, 4]  
irb(main):003:0\> a.flatten 1  
=\> [[1, 2], 3, 4]  
irb(main):004:0\>

Kind regards

robert
