Keyword arguments allow us to write code that is both more expressive, and more flexible. Tune in as Ian and Chris dive into some of the more subtle aspects and demonstrate how we can use keyword args to produce clear, reliable code.
14:21 "We basically use keyword arguments EVERYWHERE…
Okay, do you think these are money on class parameters? Seems like it is much more manageable to do an args hash and then bust defaults off into their own method. Might be just selling myself since this is what I am used to.
class Foo
def initialize(args={})
args = defaults.merge(args)
@bar = args[:bar]
@baz = args[:baz]
end
def meaning_of_life
puts '42'
end
private
def defaults(args)
{bar: 'bar', baz: 'baz'}
end
end
vs
class Foo
def initialize(bar: 'bar', baz: 'baz')
@bar = bar
@baz = baz
end
def meaning_of_life
puts '42'
end
end