Posted almost 7 years ago. Visible to the public. Repeats. Linked content.

Ruby: Do not mix optional and keyword arguments

Writing ruby methods that accept both optional and keyword arguments is dangerous and should be avoided. This confusing behavior will be deprecated in Ruby 2.7 and removed in Ruby 3, but right now you need to know about the following caveats.

Consider the following method

# DO NOT DO THIS def colored_p(object = nil, color: 'red') switch_color_to(color) puts object.inspect end colored_p(['an array']) # ['an array'] (in red) colored_p({ a: 'hash' }, color: 'blue') # {:a=>'hash'} (in blue) colored_p({ a: 'hash' }) # ArgumentError: unknown keyword: a

What happened?

Ruby does not know whether to interpret this as

colored_p({ a: 'hash' }, color: 'red')

or as

colored_p(nil, a: 'hash')

It defaults to the later, and then throws an error.

Worse, this can also happens for any arguments that define to_hash, for example

class User attr_accessor :first_name, :last_name def to_hash { first_name: first_name, last_name: last_name } end end colored_p( # ArgumentError: unknown keywords: first_name, last_name

This behavior is not very smart.

The easy fix is just to never mix optional and keyword arguments in the first place, so there can never be any ambiguity.

Your development team has a full backlog of feature requests, chores and refactoring coupled with deadlines? We are familiar with that. With our "DevOps as a Service" offering, we support developer teams with infrastructure and operations expertise.

Owner of this card:

Tobias Kraze
Last edit:
over 2 years ago
by Besprechungs-PC
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Tobias Kraze to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more