From: Markus Date: 2004-10-25T13:46:57+09:00 Subject: Re: hash << I know I'm late to the party here, but would you be happy with: class Hash alias << update end with which you could write: h << {:a => :b} << {:c => :d} << {:e => :f} -- Markus P.S. I still maintain the canonical refutation of your sig is "hug." On Wed, 2004-10-20 at 07:04, Matt Maycock wrote: > So, unless you count merge! and update, there doesn't seem to be a way > to shove stuff into a hash and get the resulting updated hash back. > Now, I know merge and update make it real easy: > > h = {} > h.update(:a => :b).update(:c => :d).update(:e => :f) > > but this some how feels unnatural to me. maybe the following is > better for me and others? > > ---code--- > require 'ext/singleton_class' > > class Hash > def <<(k) > if @shift_value.nil? then > @shift_value = Object.new.singleton_def(:key) {k} > else > key, @shift_value = @shift_value.key, nil > self[key] = k > end > self > end > end > ---end code--- > > ---usage--- > h = {} > h << :language << :ruby << :creator << :matz << :users << :"comp.lang.ruby" > p h > ---result--- > {:language=>:ruby, :creator=>:matz, :users=>:"comp.lang.ruby"} > ------------- > > Just a question of flavour, really.