javascript arguments parameter in ruby - ruby

I have a function with no parameters declared in its firm, but I need to obtain them if eventually any was passed to it.
For example, in javascript I can have a function as follows:
function plus() {
return operator("+", arguments);
}
As you can see, I can obtain the function arguments via "arguments" implicit parameter.
Does ruby have something similar to javascript argument parameter?
Thanks.
PS: I did a previous research in google, stackoverflow and this book with no result, maybe there is a workaround for this and no an official way to obtain it.

How about using variable length arguments:
def plus(*args)
# Do something with `args` array
end

In ruby you can always put optional arguments in a hash, such as
def some_function(args = {})
end
and you can call it like
some_function :arg1 => some_integer, :arg2 => "some_string"

Related

How to pass an optional method parameter as string in Ruby

I would like to call a method in Ruby, which has an optional parameter.
I tried some ways, but none of them is working.
Can you help me, how can I call this method?
I never used Ruby before, so please help me refine the question itself. I tried googling the problem, but I think I use the wrong terms.
I read this: Ruby Methods and Optional parameters and this: A method with an optional parameter, but with no luck.
The method looks like this:
def method(param1, param2, options={})
...
if options["something"]
...
end
...
end
I tried the call, for example, like this:
method("param1", "param2", :something => true)
With my tries, the code runs but does not enter in the if condition.
I would like to call this method in the way, that the codes in the if statement would be run.
It doesn't work because you are sending symbol (:something) instead of string key ('something'). They are different objects.
Change:
method("param1", "param2", :something => true)
to
method("param1", "param2", 'something' => true)
or handle in method by if options[:something]
Call your method with the same param type, or if you want to be able to pass either symbol or string key you can handle that in your method.
def foo(a,b, opt={})
if opt[:something] || opt['something']
puts 'something'
end
end
Now you can call this with string or symbol keys:
foo('a','b', 'something' => true )
#or
foo('a','b', something: true )

Function call in Ruby

I am new to ruby and I want to achieve this:
Foo.runtask param1: :asd, param2: :qwerty
I know how to create a function taking two parameters, but I want to know how to call it like I mentioned specifically "param1:" and "param2:"
param1: :asd, param2: :qwerty It is a shorthand for { param1: :asd, param2: :qwerty }, which is a Hash (In some cases, you can omit the curlies of a Hash).
If you want to pass arguments like that, you method should accept a Hash as the parameter
eg
def my_method(options)
puts options[:param1]
puts options[:param2]
end
Then you can call my_method param1: :asd, param2: :qwerty
From ruby 2.0 onwards ruby supports named arguments, so while you can still declare your method as just taking a single options argument you can also do
def foo(param1: 1, param2: :bar)
...
end
Deep down in the inside there is still a hash of arguments being passed around, but this allows you to specify default values easily (like normal default values these can be any ruby expression) and will raise an exception if you pass a names parameter other than a listed one.
In ruby 2.1 you can also have mandatory named arguments
def foo(param1: 1, param2:)
...
end
param2 is now mandatory.
In both cases you invoke the method like before:
foo(param1: :asd, param2: :qwerty)
In fact just by looking at this invocation you can't tell whether these will be consumed as named arguments or as a hash
You can of course emulate this with hashes but you end up having to write a bunch of boilerplate argument validation code repeatedly.
To tell whether a parameter is taking its default value was passed explicitly you can use this well known trick
def foo(param1: (param_1_missing=true; "foo")
...
end
Here param1 will be set to "foo" by default and param_1_missing will be true or nil

When mocking, how do I return one of the arguments that was passed in?

I have a class method which I would like to mock and return one of the arguments that was passed in. Something like this in my code:
converted_data = Myclass.convert(arg, some_other_arg, data)
And in my test I would like to be able to do the following (although this doesn't work).
Myclass.should_receive(:convert).with(*args).and_return(args[2])
So the method doesn't actually do anything! If I run as written above, I get an error that it doesn't know what args is to return it.
I found the answer:
#something.should_receive(:method).with(any_args()).and_return { |*args| args }
To return a specific argument, pick it from the array within the and_return block! So for your example, it would be:
Myclass.should_receive(:convert).with(any_args()).and_return { |*args| args[2] }
You likely want to do this on an instance of Myclass.
Use #and_return with a block:
Myclass.should_receive(:convert).with(*args).and_return {|args| args[2]}

Ruby instance_exec / instance_eval with arguments

I'm trying to dynamically call a method given in a string using parameters given in the same string, I'm getting stuck on supplying the parameters though...
I currently have:
query = Query.new
while true
input = gets.split(%r{[/[[:blank:]]/,]})
puts (query.instance_exec(*input.drop(1)) { |x|
instance_eval input.at(0)
})
end
So the method name is input(0) and the arguments to this method are in the rest of input.
Is there any way to call this method with those parameters?
The method you are looking for is send. Its first argument will be the method, and the rest will be passed to that method.
query = Query.new
puts query.send(*gets.split(/\s+/)) while true
You can use while modifier.
Your regex looks complicated. I made it look simple.
Don't forget to use the splat operator *, which decomposes an array.

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

Forgive the beginner question, but say I have an array:
a = [1,2,3]
And a function somewhere; let's say it's an instance function:
class Ilike
def turtles(*args)
puts args.inspect
end
end
How do I invoke Ilike.turtles with a as if I were calling (Ilike.new).turtles(1,2,3).
I'm familiar with send, but this doesn't seem to translate an array into an argument list.
A parallel of what I'm looking for is the Javascript apply, which is equivalent to call but converts the array into an argument list.
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)

Resources