Right now, my ruby code looks like this:
case step
when 1
# step_two_function
when 2
# step_three_function
when 3
# step_four_function
end
However, I'm wondering if I can store this in a hash to make things a little more simpler?
For example,
h = {
1 => step_two_function,
2 => step_three_function,
3 => step_four_function,
}
And be able to just simply call the associated function with something like h[step]. It seems that, in Ruby, the function is executed at the same time as the hash being created, rather than only when the value of the key is called.
Is doing this possible and, if so, how?
In python, you would just simply do:
>>> def lies(altons_var):
... print(altons_var)
...
>>> a[1] = lies
>>> a[1]('print me please')
print me please
But this doesn't work in Ruby because the minute you call the function (whether directly or from a hash), it executes the function.