It might be worth mentioning that Ruby 2.1 adds the ability to pass keyword arguments that shouldn't be in a specific order, and you can make them mandatory or have default values.
Dropping the parameter hash reduces the pattern code to retrieve the hash options. Unnecessary boilerplate code increases the possibility of typos and errors.
Also with the keyword arguments defined in the method signature itself, you can immediately find the argument names without reading the body of the method.
Mandatory arguments are followed by a colon, while arguments with default values ββare passed in the signature, as you would expect.
For instance:
class Person attr_accessor(:first_name, :last_name, :date_of_birth) def initialize(first_name:, last_name:, date_of_birth: Time.now) self.first_name = first_name self.last_name = last_name self.date_of_birth = date_of_birth end def my_age(as_of_date: Time.now, unit: :year) (as_of_date - date_of_birth) / 1.send(unit) end end
source share