Forum: Ruby Extending has for default value, can't get it to work

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
459c1abc1584e38501038b35a753e6f6?d=identicon&s=25 Jo Be (nousplacidus)
on 2009-03-07 04:23
http://pastie.org/409974

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

thanks
D7463bd611f227cfb2ef4da4a978a203?d=identicon&s=25 Christopher Dicely (Guest)
on 2009-03-07 05:09
(Received via mailing list)
On Fri, Mar 6, 2009 at 7:22 PM, Jo Be <nous.placidus@gmail.com> 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"}
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 Chris Hulan (Guest)
on 2009-03-07 05:27
(Received via mailing list)
On Mar 6, 11:07 pm, Christopher Dicely <cmdic...@gmail.com> 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
'
459c1abc1584e38501038b35a753e6f6?d=identicon&s=25 Jo Be (nousplacidus)
on 2009-03-07 05:27
Christopher Dicely wrote:
> On Fri, Mar 6, 2009 at 7:22 PM, Jo Be <nous.placidus@gmail.com> 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
=> {}
>>
459c1abc1584e38501038b35a753e6f6?d=identicon&s=25 Jo Be (nousplacidus)
on 2009-03-07 05:29
Well that was easy, thanks all
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-03-08 15:04
(Received via mailing list)
2009/3/7 Jo Be <nous.placidus@gmail.com>:
> 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

2. 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
This topic is locked and can not be replied to.