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


#1

http://pastie.org/409974

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

thanks


#2

On Mar 6, 11:07 pm, Christopher D. 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


#3

On Fri, Mar 6, 2009 at 7:22 PM, Jo Be removed_email_address@domain.invalid 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”}


#4

Well that was easy, thanks all


#5

2009/3/7 Jo Be removed_email_address@domain.invalid:

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


#6

Christopher D. wrote:

On Fri, Mar 6, 2009 at 7:22 PM, Jo Be removed_email_address@domain.invalid 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
=> {}