Ruby’s Hash#default=

by john on July 21, 2011

If Ruby’s Hash#default= returned the Hash instead of the default object, you could write:

TRANSLATION = { 'hello' => 'bonjour' }.default=('unknown')
  • Eric

    “If only, if only,” the woodpecker sighs, “The bark on the tree was just a little bit softer.”

  • Jeremy Weiskotten

    How about something like this:

    TRANSLATION = { ‘hello’ => ‘bonjour’ }.with_default(‘unknown’)

Previous post:

Next post: