Method parameter values as defaults of other optional parameters
We can define optional parameters with default values in Ruby:
def validate(username, options = {})
options
end
validate 'user'
=> {}
But did you know that we can also define the default value of an optional parameter using the value of another parameter?
def welcome(username, domain, email = "#{username}@#{domain}")
email
end
welcome 'user', 'host.com'
=> 'user@host.com'
Written by Erol Fornoles
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#