How to Inspect What Is the Default Value for Optional Parameter in Ruby's Method

How can I inspect what is the default value for optional parameter in ruby's method?

I think the reason such utility is not made available is that the values of the default arguments are evaluated when they have to be assigned. Therefore, trying to evaluate them might have side additional effects.


Let me tell you a story about the Russian government's nuclear plans:

Some time ago, they hired ultra hardcore Russian hackers to come up with a a solution that is both error-proof and mega secure that allows to either launch all available nukes or simply run a simulation. They decided to create one method called launch_all_nukes, which optionally accepts a keyword argument simulation_number:. They loaded the implementation in a REPL and deleted the code so enemy spies could never find out how it actually works.


Each day for the past couple of years, the trusted specialist Ivan travels to a giga secret location where he sits in front of what looks to be a regular irb and evaluates the chances of the Russian Federation surviving a supposed mutual assured destruction.

$: launch_all_nukes simulation_number: 1

...

Just another regular day.

$: launch_all_nukes simulation_number: 2

...

$: launch_all_nukes simulation_number: 3

...

Even though these take 25 minutes on average, it feels like hours sometimes.

$: launch_all_nukes simulation_number: 4

...

Staring at the screen. Just another regular day. Another... regular... day...

$: launch_all_nukes simulation_number: 5

...

Tik-tok, tik-tok, tik-tok... Wondering what might there be for lunch?

$: launch_all_nukes simulation_number: 6

...

Finally! 7 is always the most interesting. It's the only one that sometimes shows there is a 0.03% - 0.08% chance of not complete annihilation. Ivan has no idea what stands behind the number 7. Or any of the other simulations for that matter. He just runs commands and waits. But surely, number 7 is the one that brings little beams of joy and excitement in his otherwise dull assignment.
Aaaaaaand, go!

$: launch_all_nukes simulation_number: 7

...

0%. As all the others. How regular.

$: launch_all_nukes simulation_number: 8

...

Does it matter, actually? Why would one nation be superior to all the others? Is human life valuable by itself to begin with? Is Earth as a whole inherently valuable? Just a tiny spectacle of rock floating in an endless Universe...

$: launch_all_nukes simulation_number: 9

...

What happened? Ivan used to be a great developer. And now he just stares at a console, running repetitive commands from time to time... Is this what progress feels like...

$: launch_all_nukes simulation_number: 10

...

Wait a second... What is the default value of simulation_number:? What is it? Surely, the implementation has some check like __actually_launch_nukes__ if simulation_number.nil?. But is it really nil? Or is it something else? ...

$: launch_all_nukes simulation_number: 11

...

Like a repetitive earworm, this tiny question never left his mind... what is it? ... He never feared accidentally endangering the world because he saw that running launch_all_nukes with no arguments prompts for three different access keys, none of which he knows.

$: launch_all_nukes simulation_number: 12

...

Ivan has ran ordinary Ruby commands in the console before. By all means, it's just a regular irb... Just running one simple introspection method... He knows he is not allowed to do it... But no one will know, right? No one even knows how this program works anyway... Ah...

$: launch_all_nukes simulation_number: 13

...

13 and 14 are the worst! 13 usually takes an hour and a half. 14 is even longer. Damn it, Ivan craves, just an itsy bitsy tiny information to keep his mind engaged for at least a couple of minutes... Lets do it!

$: method(:launch_all_nukes).default_value_for(:simulation_number)

...

Mortified, Ivan froze motionless as the sudden realization hit him. He now knows what the default value is. But it is too late...


Here is a poor man's attempt:

argument_name = 'arg2'

origin_file, definition_line = MyClass.instance_method(:index).source_location
method_signature = IO.readlines(origin_file)[definition_line.pred]
eval(method_signature.match(/#{argument_name}\s*[=:]\s*\K[^\s),]*/)[0]) # => "hello"

Obviously very error prone:

  • Doesn't work with native methods
  • Doesn't work with methods defined in REPLs
  • You need read privileges
  • The regex doesn't handle many cases (like more complex default values that have whitespaces, ) or , in them), but this can be improved.

If someone comes up with a purely introspective solution, go with that.

Getting the default value of named or optional parameter

This is not possible. I can't find the thread right now, but matz has explicitly said that this is by design. The problem is that the default value is an arbitrary expression, and since everything in Ruby is an expression, it can be anything.

For example, what about this:

def foo(bar = if rand < 0.5 then Time.now else rand end)

What would your proposed method return here?

Basically, you have two choices:

  • Evaluate the default value expression. This means you will get some value, but that doesn't tell you much about what the real expression is.
  • Don't evaluate the default value expression, by packaging it up in a Proc. But there's no way to get the expression out of a Proc again.

So, either way, you don't actually get any useful information.

Optional arguments with default value in Ruby

You cannot do that (or something similar) in Ruby < 2.0. The best you could do is:

def my_function(h = {})
h[:c] ||= 500
# Use h[:a], h[:b], h[:c]
...
end

my_function(b: 100)

A method with an optional parameter

def some_func(variable = nil)
...
end

How to determine if optional arguments are passed in Ruby

I am not sure this is possible. Consider the following:

class Test
def method(a,b=1)
local_variables.each do |var|
puts "Setting #{var} to #{eval var.to_s}"
end
end
end

And then try to call method on an instance of Test:

?> t.method(1)
Setting a to 1
Setting b to 1
=> [:a, :b]

?> t.method(1,2)
Setting a to 1
Setting b to 2
=> [:a, :b]

?> t.method(1,1)
Setting a to 1
Setting b to 1
=> [:a, :b]

Those do not distinguish how the method was called.

So I would suggest @sawa's approach mixed with the following:

raise ArgumentError, "Too many arguments" if args.length > 2

since you seem to want to limit the parameters to 2 and @sawa's approach allows for an undefined number.

This is an unorthodox approach to an unorthodox requirement, so just make sure the clients of your method are aware of how the API works.

How to decide whether an optional argument was given or not in a ruby method

Jorg W Mittag has the following code snippet that can do what you want:

def foo(bar = (bar_set = true; :baz))
if bar_set
# optional argument was supplied
end
end

Is there an idiomatic way to specify default values for optional parameters in Ruby?

A common pattern is to use

def foo(options = {})
options = { :default => :value }.merge(options)
end

You’ll end up with options being a hash containing the passed in values, with the options from your defaults hash as any that weren’t provided.



Related Topics



Leave a reply



Submit