From: Adam Prescott Date: 2013-03-30T01:02:40+09:00 Subject: Re: Hash with default On 29 March 2013 15:53, Julian Leviston wrote: > h = {"two"=>2,"three"=>3}; def h.[](key); self.has_key?(key) ? super(key) : key; end Why not use the block to Hash.new instead of overriding #[] ? >> h = Hash.new { |h,k| k } => {} >> h["one"] => "one" >> h => {} Note this is different than using h[k] = k in the block. As for the original question, I think you can rely on default_proc and get around the fact it doesn't return the receiver pretty easily. >> h = { "two" => 2, "three" => 3 }.tap { |o| o.default_proc = lambda { |h, k| k } } => {"two"=>2, "three"=>3} >> h => {"two"=>2, "three"=>3} >> h["two"] => 2 >> h["five"] => "five" >> h => {"two"=>2, "three"=>3} Having said that, I don't see any benefit in trying to get it down to a single line and sacrificing readability. You can define h to be what you want, then on a separate line just call #default_proc=.