Ruby Keyword Arguments - mandatory params
If you do not set a default value for a keyword argument it will become mandatory and will raise an ArgumentError
.
an example:
def mandatory_keyword(bar:)
"foo=#{bar}"
end
def default_keyword(bar: nil)
"foo=#{bar}"
end
and its test:
describe 'keyword_arguments' do
it { expect{mandatory_keyword()}.to raise_error(ArgumentError) }
it { expect(mandatory_keyword(bar: 'bar')).to eq('foo=bar') }
it { expect(default_keyword()).to eq('foo=') }
it { expect(default_keyword(bar: 'bar')).to eq('foo=bar') }
end
In case you don't want this behavior you can always set a default to nil.
Tweet