[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Jo Be

3/7/2009 3:22:00 AM

http://pastie....

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

thanks
--
Posted via http://www.ruby-....

5 Answers

Christopher Dicely

3/7/2009 4:07:00 AM

0

On Fri, Mar 6, 2009 at 7:22 PM, Jo Be <nous.placidus@gmail.com> wrote:
> http://pastie....
>
> 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"}

Chris Hulan

3/7/2009 4:22:00 AM

0

On Mar 6, 11:07 pm, Christopher Dicely <cmdic...@gmail.com> wrote:
> On Fri, Mar 6, 2009 at 7:22 PM, Jo Be <nous.placi...@gmail.com> wrote:
> >http://pastie....
>
> > 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"}

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
'

Jo Be

3/7/2009 4:26:00 AM

0

Christopher Dicely wrote:
> On Fri, Mar 6, 2009 at 7:22 PM, Jo Be <nous.placidus@gmail.com> wrote:
>> http://pastie....
>>
>> 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-....

Jo Be

3/7/2009 4:28:00 AM

0

Well that was easy, thanks all
--
Posted via http://www.ruby-....

Robert Klemme

3/8/2009 2:02:00 PM

0

2009/3/7 Jo Be <nous.placidus@gmail.com>:
> http://pastie....
>
> 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

--
remember.guy do |as, often| as.you_can - without end