Sometimes you rediscover little features that make your life whole lot easier. Today when reviewing the code of my co-worker I stumbled upon the use of the “fetch” on the Hash object. Check it:
1234567
h={:existing_key=>"value"}h.fetch(:existing_key)# returns "value"h.fetch(:some_key)# raises exceptionh.fetch(:some_key,"default")# returns "default"h.fetch(:some_key){...somemath...}# returns some math