On Sun, Oct 29, 2006 at 02:35:02PM +0900, Joel VanderWerf wrote: > Joe Ruby MUDCRAP-CE wrote: > >Is there a way for us lazy typists to define a hash using %q? I tried > >this, but it doesn't create a hash: > > > >h = %q(a => b, c => d) > > irb(main):006:0> Hash[*%w{ a b c d }] > => {"a"=>"b", "c"=>"d"} > > More decadent: > > irb(main):008:0> def mkh arg; Hash[*arg]; end > => nil > irb(main):009:0> mkh %w{ a b c d } > => {"a"=>"b", "c"=>"d"} > > Ultra decadent: > > irb(main):017:0> class Array; def -@; Hash[*self]; end; end > => nil > irb(main):018:0> -%w{ a b c d } > => {"a"=>"b", "c"=>"d"} > Alternative decandence: class Array def to_hsh require 'enumerator' to_enum(:each_slice, 2).to_a.inject({}) { |h, (k, v)| h.update(k=>v) } end end %w( a b c d ).to_hsh > -- > vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407