Ruby programmatically calling method, with variable number of args - ruby

I am trying to do something similar to this:
def foo(mode= :serial)
if (mode == :serial) then
self.send(:bar, "one_type")
else
self.send(:bar,"second_type",:T5)
end
end
I can obviously type this out like this.
But I've recently tried expanding it to include a second function like this:
def foo(mode= :serial)
if (mode == :serial) then
self.send(:bar, "one_type")
self.send(:foobar, "one_type",20)
else
self.send(:bar,"second_type",:T5)
self.send(:foobar, "one_type",20,:T5)
end
end
I can still continue as it is, but I thought to myself, there's a pattern here, I should abstract the arguments away into another layer and make this simpler.
So what I wanted to do was something like this:
arg_arr = [:bar, "one_type"]
arg_arr_2 = [:foobar, "one_type", 20]
if (mode == :serial) then
self.send(arg_arr)
self.send(arg_arr_2)
else
arg_arr << :T5
arg_arr2 << :T5
self.send(arg_arr)
self.send(arg_arr2 )
end
I tried some other ideas involving .each, .inspect, but nothing that would work (the usual error was can't convert array into string, which I'm guessing refers to the fact that it treats the array as the entire function name). I can do it if I explicitly say "use array elements[0] , [1] etc, but that just seems wasteful.
Is there a way to achieve this without writing code that is hardcoded to the number of arguments?

Try this
def foo(a, b)
puts a
puts b
end
array = ['bar', 'qux']
send(:foo, *array) # using send
foo(*array) # using the method name
Both print
bar
qux
The splat operator * packs or unpacks an array.

Some years ago I did what you are trying now. With an asterisk in front of a method parameter you can receive as many parameters as you want in a function. So You don't need to know the number of the given parameters. It's called a splat.
Send your values as an array with an asterisk in front too and it will work.
I tested the folling with an irb console:
def test(*args)
puts args.inspect
end
my_args = [1, 2, 3]
self.send(:test, *my_args)
# [1, 2, 3]
# => nil
Or send as many single parameters as you want:
self.send(:test, 'a', 'b', 'c', 'd')
# ["a", "b", "c", "d"]
# => nil
If you have a fixed number of parameters this will work:
def test(arg1, arg2, arg3)
puts arg1.inspect
puts arg2.inspect
puts arg3.inspect
end
my_args = [1, 2, 3]
self.send(:test, *my_args)
# 1
# 2
# 3
# => nil

First, you shouldn't use send. You could use public_send, Method#call or just bar(...) if you know the method name.
Homogenous parameters
If the parameters are homogenous (e.g. are instances of the same Class), you can just put them in an Array, and use this Array as parameter :
def analyze_array(array)
puts "Elements : #{array}"
puts "Length : #{array.size}"
puts "Sum : #{array.inject(:+)}"
puts
end
analyze_array([1,2,3])
analyze_array([1,2,3,4,5])
It outputs :
Elements : [1, 2, 3]
Length : 3
Sum : 6
Elements : [1, 2, 3, 4, 5]
Length : 5
Sum : 15
Example
Refactoring your code a bit, it could become :
arg_arr = [:bar, 1]
arg_arr_2 = [:foobar, 1, 2]
def bar(array)
puts " bar with one parameter : #{array}"
end
def foobar(array)
puts " foobar with one parameter : #{array}"
end
[:serial, :parallel].each do |mode|
puts "Mode : #{mode}"
[arg_arr, arg_arr_2].each do |method_and_args|
method_name, *args = method_and_args
args << 3 if mode != :serial
method(method_name).call(args)
end
end
It outputs :
Mode : serial
bar with one parameter : [1]
foobar with one parameter : [1, 2]
Mode : parallel
bar with one parameter : [1, 3]
foobar with one parameter : [1, 2, 3]
Heterogenous parameters
For an unknown number of parameters that might belong to different classes, you can use the splat operator (documentation) :
def analyze_parameters(*params)
puts "Parameters : #{params}"
puts "Number : #{params.size}"
puts "Classes : #{params.map(&:class)}"
end
analyze_parameters('Test')
analyze_parameters(1, 'a', :b, [:c, :d])
It outputs :
Parameters : ["Test"]
Number : 1
Classes : [String]
Parameters : [1, "a", :b, [:c, :d]]
Number : 4
Classes : [Fixnum, String, Symbol, Array]
Your example becomes :
arg_arr = [:bar, 1 ]
arg_arr_2 = [:foobar, 1, 'a']
def bar(*params)
puts " bar with multiple parameters : #{params}"
end
def foobar(*params)
puts " foobar with multiple parameters : #{params}"
end
[:serial, :parallel].each do |mode|
puts "Mode : #{mode}"
[arg_arr, arg_arr_2].each do |method_and_args|
method_name, *args = method_and_args
args << :b if mode != :serial
method(method_name).call(*args)
end
end
It outputs :
Mode : serial
bar with multiple parameters : [1]
foobar with multiple parameters : [1, "a"]
Mode : parallel
bar with multiple parameters : [1, :b]
foobar with multiple parameters : [1, "a", :b]

Related

How do I call a ruby function named []?

I am new to Ruby, so please excuse this question if it is obvious.
I am working with a Module with a function signature that I don't understand. How would I call this function?
module Facter
...
def self.[](name)
collection.fact(name)
end
...
In my code I want to reference something that should be in collection.fact, in this Facter module. What syntax to I use to call this function?
Cheers
It works like this:
class MyModule
def self.[](arg)
puts arg
end
end
MyModule["Hello world"] # will print Hello world
Please see official docs:
https://ruby-doc.org/core/doc/syntax/methods_rdoc.html
Additionally, methods for element reference and assignment may be defined: [] and []= respectively. Both can take one or more arguments, and element reference can take none.
class C
def [](a, b)
puts a + b
end
def []=(a, b, c)
puts a * b + c
end
end
obj = C.new
obj[2, 3] # prints "5"
obj[2, 3] = 4 # prints "10"
So about example from docs
# From docs
obj[2, 3]
# It's the same as
obj.[](2, 3)
More interesting example
# From docs
obj[2, 3] = 4
# will print 10
# => 4
# It's the almost as
obj.[]=(2, 3, 4)
# will print 10
# => nil
As you see when you call as obj[2, 3] = 4 Ruby takes the value after = as the last argument of the []= method and return it as method result
And regardless of whether there is return in the method body. For example
class C
def []=(a, b, c)
puts "Before return"
return 12
puts "After return"
end
end
obj = C.new
obj[2, 3] = 4
# will print Before return
# => 4
obj.[]=(2, 3, 4)
# will print Before return
# => 12
It is desirable to define such method with more than one parameter. Technically, you can have only one, but the call will be like this obj[] = 1

Why index self instead of instance variable in bracket method

I have:
class Thing
def initialize
#array = [[0, 0, 0], [1, 1, 1]]
end
end
thing = Thing.new
The normal way to access an element in #array is to use [] as in:
#array[0][1] # => 0
I am trying to overwrite [] so as to get results like this:
position_array = [0, 1]
#array[position_array] # => 0
This is my attempt:
class Thing
def [](position_array)
index_row, index_col = position_array
#array[index_row][index_col]
end
def get_value(position_array)
#array[position_array] # doesn't work
# self[position_array] # does work
end
end
thing.get_value([0, 1])
# >> 'get_value': no implicit conversion of Array into Integer (TypeError)
Why do I need to index the Thing object in order to index #array?
Just think of message and receiver.
#array[position_array] sends the message [] to the receiver #array. #array is an instance of Array, so the method Array#[] gets invoked.
self[position_array] sends the message [] to the receiver self. Within instance methods, self refers to that instance. And because self is an instance of Thing, the method Thing#[] gets invoked.
Since Thing is a subclass of Object and not a subclass of Array (nothing wrong here, you shouldn't subclass Array anyway), your implementation of [] does not override Array#[]. Both methods are totally independent of each other, just like String#[] or Hash#[].
This is how I would approach it:
class Thing
def initialize
#array = [[1, 2, 3], [4, 5, 6]]
end
def [](i, j)
#array[i][j]
end
end
thing = Thing.new
thing[0, 1] #=> 2
thing[1, 1] #=> 5
You could use a prepended method to non-invasively override the [] method in Array by duck-typing the parameter passed to the [] method, and then calling the original if its not what you expect. Then you don't need a Thing object at all.
module MyArrayExtension
def [] (*param)
if param.size == 2
row, col = param
raise ArgumentError, 'Row must be an integer' if row.class != Integer
raise ArgumentError, 'Column must be an integer' if col.class != Integer
raise ArgumentError, "Element at row #{row} is not an array" if self[row].class != Array
self[row][col]
else
super
end
end
end
class Array
prepend MyArrayExtension
end
thing = [[1,2,3],[4,5,6]]
puts "The 2D array is: #{thing}"
puts "Extension used on the thing to get at element 1 of first array:"
puts thing[0,1]
puts '-' * 20
normal = [1,2,:blah,4,5]
puts "Normal array is #{normal}"
puts "Original [] method used to get the 3rd element:"
puts normal[2]
puts '-' * 20
puts "Using the extension on the non-2D array:"
puts normal[0,1]
The output of this program is:
The 2D array is: [[1, 2, 3], [4, 5, 6]]
Extension used on the thing to get at element 1 of first array:
2
--------------------
Normal array is [1, 2, :blah, 4, 5]
Original [] method used to get the 3rd element:
blah
--------------------
Using the extension on the non-2D array:
./test.rb:9:in `[]': Element at row 0 is not an array (ArgumentError)
from ./test.rb:35:in `<main>'

Why does my function not print the sorted array?

# Methods for calculating
# print out the input that the user entered
def PrintScores(*numbers)
numbers.each {|x| print x.join(" ")}
puts
end
#print out the scores in ascending order
def ListScores(*numbers)
numbers.sort!
print numbers
end
# Main function
out_file = File.new("out.txt", "w")
puts "Enter the scores you wish to have our stats program look into? "
user_input = gets.chomp
input_array = user_input.split(" ")
input_array.map! do |x|
x.to_i
end
PrintScores(input_array)
ListScores(input_array)
The ListScores function still prints the array in the order in which I entered it, and I can not figure out why.
The ListScores function still prints the array in the order in which I entered it and i can not figure out why?
In your current code, input_array is an instance of Array class which is passed as an argument to ListScores method. ListScores is expecting splat arguments, so numbers become an Array containing a single Array element(i.e., input_array contents). This is the reason you see the array in the same order when you try to sort it.
For example:
> user_input = gets.chomp
3 2 8 5 1
=> "3 2 8 5 1"
> input_array = user_input.split(" ")
=> ["3", "2", "8", "5", "1"]
> input_array.map! do |x|
> x.to_i
> end
=> [3, 2, 8, 5, 1]
> ListScores(input_array)
[[3, 2, 8, 5, 1]] => nil ## Notice Array with single Array element [[]]
splat operator(*) is used in methods when you have a need for variable parameter list.
In your case, you don't need splat operator in PrintScores and ListScores method.
def PrintScores(numbers) ## <-- Removed splat operator
numbers.each {|x| print x.join(" ")}
puts
end
#print out the scores in ascending order
def ListScores(numbers) ## <-- Removed splat operator
numbers.sort!
print numbers
end
Sample output:
> ListScores(input_array)
[1, 2, 3, 5, 8] => nil
NOTE: Its advisable to use snake_case for method name like list_scores instead of ListScores

Ruby : Rubeque - difficult with *n or n

I'm doing http://www.rubeque.com/problems/queue-continuum/solutions/51a26923ba804b00020000df and I spent a while there. I can't realize why this code doesn't pass
def initialize(queue)
#q = queue
end
def pop(n=1)
#q.shift(n)
end
def push(arr)
arr.each { |x|
#q.push(x)
}
return true
end
def to_a
#q
end
but this works perfectly.
def initialize(queue)
#q = queue
end
def pop(*n)
#q.shift(*n)
end
def push(arr)
#q.push(*arr)
return true
end
def to_a
#q
end
i'm totally confused about
def pop(*n)
#q.shift(*n)
end
and
def push(arr)
#q.push(*arr)
end
why should I take (arr) as array and than change it into... *arr which is Array of array? I'm confused, please help!
The splat works in two ways.
When receiving arguments, it combines arguments into an array.
def foo *args; args end
foo(1) # => [1]
foo(1, 2, 3) # => [1, 2, 3]
When giving arguments, it decomposes an array into arguments.
def bar x, y, z; y end
bar(*[1, 2, 3]) # => 2
def baz x; x end
baz(1) # => [1]
baz(1, 2, 3) # => Error
The *arr you are wondering is the latter case. It is not an object like [1, 2, 3] (hence, not an array of arrays). It is a part of arguments (like 1, 2, 3) passed to a method.
There are other uses of splats (as in array literals, case statements, etc.), but their function is either of the two uses above.

Return a value from a block without returning from method

I have a class Test:
class Test
attr_accessor :data
def initialize
#data = [0, 1, 2, 3]
end
def map
#data.map!{|i| i = yield i }
end
end
and I attempt to use it like:
a = Test.new
a.map{|i|
if(i==2)
i+=1
break i #<--- -This line is the focus
else
1
end
}
puts a.data
The output I expect is [1, 1, 3, 3]. Instead, I get [1, 1, 2, 3]. The last iteration of the block in map doesn't return the modified i.
I replaced break i with next i. This performed as I expected, and produced the output [1, 1, 3, 1].
How can I modify this piece of code (or, ideally the line I point out in my second code-snippet) so that I would get the output [1, 1, 3, 3]? In other words, how can I make the block finish, but pass one last value back to map? Is there a neat and readable way to do this (besides, say, toggling a boolean flag break_now)?
I'm assuming you're asking how to leave a block and make use of the last value that was calculated rather than how to calculate a specific set of numbers; for the latter, there is probably a clever one-liner.
How about something like this:
class Test
attr_accessor :data
def initialize
#data = [0, 1, 2, 3]
end
def modify
#data.map! {|i| yield i }
end
end
a = Test.new
a.modify do |i|
break i if #done
#done = i == 2
#done ? (i + 1) : 1
end
puts a.data
An additional thought—#map is an important method in Ruby with a specific interface. In your example you're violating the interface by modifying a field in Test. For this reason I've used the name #modify instead.
In general, you could get away with this by modifying the yielded values in place. For example, if your array consisted of Strings instead of Fixnums:
class Test
attr_accessor :data
def initialize
#data = %w{a b c d}
end
def map
#data.map! { |i| yield i }
end
end
a = Test.new
a.map do |i|
if i == 'c'
i.next!
break
else
'b'
end
end
p a.data #=> ["b", "b", "d", "d"]
The problem with your example is this:
Fixnum objects have immediate value. This means that when they are assigned or passed as parameters, the actual object is passed, rather than a reference to that object. Assignment does not alias Fixnum objects. There is effectively only one Fixnum object instance for any given integer value…
Fixnums can't be altered in-place, so your expression i += 1 in the lower block doesn't affect the value of i in the upper block. That's why you get 2 in your example but d in my example.
You have to do this:
a.map{ |i| (i % 2 == 0) ? i + 1 : i }
When you use map function you don't change 'a' variable, if you want change 'a' variable do this:
a.map!{ |i| (i % 2 == 0) ? i + 1 : i }
The new value of 'i' is the value return by the block, so don't do something like:
a.map{|i| i = 1 }
because if you do:
a.map{|i| i = 1; 5 }
the result will be:
[5, 5, 5, 5]

Resources