From: Jo Be Date: 2009-03-07T13:26:08+09:00 Subject: Re: Extending has for default value, can't get it to work Christopher Dicely wrote: > On Fri, Mar 6, 2009 at 7:22 PM, Jo Be wrote: >> http://pastie.org/409974 >> >> When I instantiate MyHash.new I get an empty hash. Just not sure how to >> proceed. >> >> thanks > > ruby 1.8.6 (2008-03-03 patchlevel 114) [i386-mswin32] > > irb(main):001:0> class MyHash < Hash > irb(main):002:1> def initialize > irb(main):003:2> super > irb(main):004:2> self[:foo]="bar" > irb(main):005:2> end > irb(main):006:1> end > => nil > irb(main):007:0> MyHash.new > => {:foo=>"bar"} > > Actually, I tried it your way out of curiosity, and: > > irb(main):009:0> class MyOtherHash < Hash > irb(main):010:1> def initialize > irb(main):011:2> super > irb(main):012:2> merge!({:foo=>"bar"}) > irb(main):013:2> end > irb(main):014:1> end > => nil > irb(main):015:0> MyOtherHash.new > => {:foo=>"bar"} I don't know what I'm dong wrong but I'm not getting anything from either: $ irb >> class MyHash < Hash >> ?> def intialize >> super >> self[:foo] = "bar" #merge!({:foo =>"bar" }) >> ?> end >> end => nil >> MyHash.new => {} >> ?> class MyHash2 < Hash >> ?> def intialize >> super >> self[:foo] = "bar" #merge!({:foo =>"bar" }) >> ?> end >> end => nil >> MyHash2.new => {} >> -- Posted via http://www.ruby-forum.com/.