Input arguments as array

# Normal approach
Def Foo(arg1, arg2, arg3 , arg4)
Do smthing
End #def

Sorry for miss clicking. i want to know how to input multi arguments as an
array

Def Foo(arg1, arg2, arg3 , arg4)
Do smthing
End #def
# Normal
puts ArrayofInputs.map {|x| Foo(x[0],x[1],x[2],x[3])} # specified every
argument
#Desired
puts ArrayofInputs.map {|x| Foo(x)} # input x as array of 4 arguments

Thanks

···

On Sat, Jan 13, 2018 at 11:23 PM, ng khanh <cnkhanh1986@gmail.com> wrote:

# Normal approach
Def Foo(arg1, arg2, arg3 , arg4)
Do smthing
End #def

# Normal

puts ArrayofInputs.map {|x| Foo(x[0],x[1],x[2],x[3])} # specified every
argument
#Desired
puts ArrayofInputs.map {|x| Foo(x)} # input x as array of 4 arguments

Taking out a bunch of stuff that doesn’t change, it looks like you want
some way to transform an array, given as a method argument, into its
elements. That’s the * operator, often referred to as “splat”. For
example, if you have:

def printarg(arg)
  puts arg
end

and you do printarg([1, 2, 3]), you will get [1, 2, 3]. However, if you do
printarg(*[1, 2, 3]), you will get an ArgumentError, because you’re now
feeding it three args instead of one. Modify it to accept three args and
you will get them back... but now the original call won’t work any more.
Fortunately you can also apply the * on the receiving side, so that all
args are put into an array under the parameter name... but it’s late and I
need to crash so I’ll let you investigate that yourself for a while. :wink:

···

ng khanh <cnkhanh1986@gmail.com> wrote:

--

Sent from Gmail Mobile; please excuse top posting, typos, etc. :frowning:

So you always get a kernel panic when tired? :wink: I hope you reboot safely.

Cheers

robert

···

On Sun, Jan 14, 2018 at 7:05 AM, Dave Aronson <ruby-talk.list.2.TRex@codosaur.us> wrote:

but it’s late and I need to crash so I’ll let you investigate that yourself for a while. :wink:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/