Ruby Convert Array into Function Arguments

ruby convert array into function arguments

You can turn an Array into an argument list with the * (or "splat") operator:

a = [0, 1, 2, 3, 4] # => [0, 1, 2, 3, 4]
b = [2, 3] # => [2, 3]
a.slice(*b) # => [2, 3, 4]

Reference:

  • Array to Arguments Conversion

Given an array of arguments, how do I send those arguments to a particular function in Ruby?

As you know, when you define a method, you can use the * to turn a list of arguments into an array. Similarly when you call a method you can use the * to turn an array into a list of arguments. So in your example you can just do:

Ilike.new.turtles(*a)

Sending elements of an array as arguments to a method call

try calling it like this

hello(arr1, *arr2)

here's a run through in irb

irb(main):002:0> def hello(foo, *bar)
irb(main):003:1> puts foo.inspect
irb(main):004:1> puts bar.inspect
irb(main):005:1> end
=> nil
irb(main):006:0> arr1 = ['baz', 'stuff']
=> ["baz", "stuff"]
irb(main):007:0> arr2 = ['ding', 'dong', 'dang']
=> ["ding", "dong", "dang"]
irb(main):008:0> hello(arr1, arr2)
["baz", "stuff"]
[["ding", "dong", "dang"]]
=> nil
irb(main):009:0> hello(arr1, *arr2)
["baz", "stuff"]
["ding", "dong", "dang"]
=> nil

by adding the * to the second array, it treats them as an array instead of an array of an array, which is what you're looking for i think

How to pass array as an argument to a Ruby function from command line?

You clearly want to pass an array as the first parameter into start_services method, so you should do it like this:

$ ruby -r "./test.rb" -e "start_services ['dev','test','uat'],'developer','welcome123'"
# output:
dev
test
uat
developer
welcome123

What you've been trying so far was attempt to pass '[\'dev\',\'test\',\'uat\']' string, which was malformed, because you didn't escape ' characters.

How to define a Ruby method that either takes an Array or a String?

Why not something like this:

def list(*projects)
projects.join(', ')
end

Then you can call it with as many arguments as you please

list('a')
#=> "a"
list('a','b')
#=> "a, b"
arr = %w(a b c d e f g)
list(*arr)
#=> "a, b, c, d, e, f, g"
list(arr,'h','i')
#=> "a, b, c, d, e, f, g, h, i"

The splat (*) will automatically convert all arguments into an Array, this will allow you to pass an Array and/or a String without issue. It will work fine with other objects too

list(1,2,'three',arr,{"test" => "hash"}) 
#=> "1, 2, three, a, b, c, d, e, f, g, {\"test\"=>\"hash\"}"

Thank you @Stefan and @WandMaker for pointing out Array#join can handle nested Arrays

How do I pass multiple arguments to a ruby method as an array?

Ruby handles multiple arguments well.

Here is a pretty good example.

def table_for(collection, *args)
p collection: collection, args: args
end

table_for("one")
#=> {:collection=>"one", :args=>[]}

table_for("one", "two")
#=> {:collection=>"one", :args=>["two"]}

table_for "one", "two", "three"
#=> {:collection=>"one", :args=>["two", "three"]}

table_for("one", "two", "three")
#=> {:collection=>"one", :args=>["two", "three"]}

table_for("one", ["two", "three"])
#=> {:collection=>"one", :args=>[["two", "three"]]}

(Output cut and pasted from irb)

pass array into vararg in ruby?

You can use the splat operator like this:

exec("echo", *["hello","world"])

How does passing block to a new array works in terms of function arguments (to where exactly we are passing it)?

Passing a block to Array.new works the same way as passing a block to any other method: It's a sort of implicit argument. It does not appear in the method's argument list (unless you "reify" it into a Proc using &), but you can call it using yield and check whether it is there using block_given?.

For example if your some_func method were to take a block, it could look like this:

def some_func(a,b,c)
if block_given?
yield a+b+c
else
a+b+c
end
end

some_func(1,2,3) #=> 6
some_func(1,2,3) {|x| x*2} #=> 12

How to pass array elements as separate method arguments in Ruby?

yes, just use * before array:

my_method(model_name, *["x", "y", "z"])

it will result in:

my_method(model_name, "x", "y", "z")

* is a splat operator.



Related Topics



Leave a reply



Submit