Given an Array of Arguments, How to Send Those Arguments to a Particular Function in Ruby

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)

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.

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

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

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)

Passing Ruby array elements to a method

Take a look at the documentation for ruby's Enumerable, which arrays implement. What you're looking for is the map method, which takes each element of an enumerable (i.e. an array) and passes it to a block, returning a new array with the results of the blocks. Like this:

array.map{|element| resolve_name(element) }

As an aside, in your method, you do not need to use a local variable if all you're doing with it is returning its value; and the return statement is optional - ruby methods always return the result of the last executed statement. So your method could be shortened to this:

def resolve_name(ns_name)
Resolv.getaddress(ns_name)
end

and then you really all it's doing is wrapping a method call in another. So ultimately, you can just do this (with array renamed to ns_names to make it self-explanatory):

ns_names = ['ns-1.me.com', 'ns-2.me.com']
ip_addresses = ns_names.map{|name| Resolv.getaddress(name) }

Now ip_addresses is an array of IP addresses that you can use in your template.

Passing array to ruby function

To understand that, you have to make a distinction between a variable and what this variable stands for. Consider the following example :

items = [1, 2, 3]
# the variable items points to an array we just instantiated
items = [4, 5, 6]
# items now points to a new array

If ruby passed arguments by reference, doing so within a method with an argument it received would also make the variable exposed to the method by the caller point to a new location

items = [1, 2, 3]
def my_method array
array = [4, 5, 6]
return array
end
my_method(items) # --> [4, 5, 6]
items # --> [1, 2, 3]
# if ruby passed arguments by reference, items would now be [4, 5, 6]

Now ruby passes arguments by value, but the value you received is a reference to the same location in memory as the one the called passed you. In other words, you don't get a clone, a dup, but the same object.

items = [1, 2, 3]
def my_method array
array << 4
return array
end
my_method(items) # --> [1, 2, 3, 4]
items # --> [1, 2, 3, 4]

If you want your method to have no side-effect on its arguments you can clone them.

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.

In Ruby, can I pass each element of an array individually to a method that accepts *args?

You can do this:

my_array = ['a', 'b', 'c']
bar(*my_array)

This will flatten out the array into it's individual elements and pass them to the method as separate arguments. You could do this to any kind of method, not only ones that accept *args.

So in your case:

bar *arr


Related Topics



Leave a reply



Submit