From: "Jesús Gabriel y Galán" Date: 2010-04-22T00:52:29+09:00 Subject: Re: Function accepting argument list or array On Wed, Apr 21, 2010 at 5:45 PM, Thomas Allen 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.