# Extending has for default value, can't get it to work

**URL:** <https://www.ruby-forum.com/t/extending-has-for-default-value-cant-get-it-to-work/162232>\
**Category:** Ruby\
**Created:** [March 7, 2009, 4:23am UTC](https://www.ruby-forum.com/t/extending-has-for-default-value-cant-get-it-to-work/162232 "2009-03-07T04:23:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nousplacidus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@nousplacidus](https://www.ruby-forum.com/u/nousplacidus)\
**Post date:** [March 7, 2009, 4:23am UTC](https://www.ruby-forum.com/t/extending-has-for-default-value-cant-get-it-to-work/162232/1 "2009-03-07T04:23:59Z")

</div>

[http://pastie.org/409974](http://pastie.org/409974)

When I instantiate MyHash.new I get an empty hash. Just not sure how to  
proceed.

thanks

---

<div class="post-metadata">

**Author:** ![nousplacidus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@nousplacidus](https://www.ruby-forum.com/u/nousplacidus)\
**Post date:** [March 7, 2009, 5:27am UTC](https://www.ruby-forum.com/t/extending-has-for-default-value-cant-get-it-to-work/162232/2 "2009-03-07T05:27:03Z")

</div>

On Mar 6, 11:07 pm, Christopher D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):001:0\> class MyHash \< Hash
> 
> 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”}

Doh! I copied the pastie and did see the typo unitl I copied the  
correct code, thanks Chris  
You need ‘initialize’ instead of 'intialize

cheers  
’

---

<div class="post-metadata">

**Author:** ![nousplacidus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@nousplacidus](https://www.ruby-forum.com/u/nousplacidus)\
**Post date:** [March 7, 2009, 5:09am UTC](https://www.ruby-forum.com/t/extending-has-for-default-value-cant-get-it-to-work/162232/3 "2009-03-07T05:09:32Z")

</div>

On Fri, Mar 6, 2009 at 7:22 PM, Jo Be [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://pastie.org/409974](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”}

---

<div class="post-metadata">

**Author:** ![nousplacidus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@nousplacidus](https://www.ruby-forum.com/u/nousplacidus)\
**Post date:** [March 7, 2009, 5:29am UTC](https://www.ruby-forum.com/t/extending-has-for-default-value-cant-get-it-to-work/162232/4 "2009-03-07T05:29:44Z")

</div>

Well that was easy, thanks all

---

<div class="post-metadata">

**Author:** ![nousplacidus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@nousplacidus](https://www.ruby-forum.com/u/nousplacidus)\
**Post date:** [March 8, 2009, 3:04pm UTC](https://www.ruby-forum.com/t/extending-has-for-default-value-cant-get-it-to-work/162232/5 "2009-03-08T15:04:28Z")

</div>

2009/3/7 Jo Be [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> [http://pastie.org/409974](http://pastie.org/409974)
> 
> When I instantiate MyHash.new I get an empty hash. Just not sure how to  
> proceed.

If initializing the Hash with particular values is the only reason for  
a subclass, I would not choose this approach. You could rather do any  
of these

1. cloning

MyHash = {:foo =\> “bar”.freeze}.freeze

new\_hash = MyHash.dup

1. method

def Hash.my  
{:foo =\> “bar”}  
end

new\_hash = Hash.my

IMHO a subclass is only worthwhile if you add functionality. But even  
then I’d probably rather consider delegation over inheritance.  
Another alternative is to create a module with additional  
functionality. It all depends on what you want to achieve.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![nousplacidus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@nousplacidus](https://www.ruby-forum.com/u/nousplacidus)\
**Post date:** [March 7, 2009, 5:27am UTC](https://www.ruby-forum.com/t/extending-has-for-default-value-cant-get-it-to-work/162232/6 "2009-03-07T05:27:43Z")

</div>

Christopher D. wrote:

> On Fri, Mar 6, 2009 at 7:22 PM, Jo Be [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > [http://pastie.org/409974](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  
> > =\> {}
