Why do Ruby's default parameter values not get assigned to nil arguments?
The default parameter is used when the parameter isn't provided.
If you provide it as nil
, then it will be nil
. So yes, this is expected behavior.
If you want to set a default value, even if nil is passed, and still allow calling the method without an argument you need to set the default value to nil and use the "or equals" operator:
def foo(bar=nil)
bar ||= "default value"
puts bar
end