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')

{ 2 comments… read them below or add one }

Eric July 21, 2011 at 12:12 pm

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

Jeremy Weiskotten August 4, 2011 at 10:46 am

How about something like this:

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

https://gist.github.com/1125319

Leave a Comment

Previous post: Use of the word “bind” in JavaScript libraries

Next post: Spying on Ruby’s Net::HTTP