At the current moment (Ruby 2.0.0-preview1) you could use the following method signature:
def say(greeting: greeting_to_say)
puts greeting
end
The greeting_to_say
is just a placeholder which won't be evaluated if you supply an argument to the named parameter. If you do not pass it in (calling just say()
), ruby will raise the error:
NameError: undefined local variable or method `greeting_to_say' for (your scope)
However, that variable is not bound to anything, and as far as I can tell, cannot be referenced from inside of your method. You would still use greeting
as the local variable to reference what was passed in for the named parameter.
If you were actually going to do this, I would recommend using def say(greeting: greeting)
so that the error message would reference the name you are giving to your parameter. I only chose different ones in the example above to illustrate what ruby will use in the error message you get for not supplying an argument to the required named parameter.
Tangentially, if you call say('hi')
ruby will raise ArgumentError: wrong number of arguments (1 for 0)
which I think is a little confusing, but it's only preview1.