[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to re-replace Hash default value behaviour?

Siep Korteling

5/9/2008 12:47:00 AM

Hash.new(0) and the block form of Hash.new{_smart_stuff_} come in
really handy while reading in a hash.
When the reading is done however, my hash serves to provide data. At
that stage, the default_value behaviour gets in the way. For instance,
if the hash contains the number of goals for soccerplayers, I don't want
the hash to return value "0" for the key "Wolfgang Amadeus Mozart"; by
then I want plain old Nil, or "No data available".

failed experiment:

Class Hash
def default_proc
Nil
end
end

How can Hash.new behaviour be changed?

regards,
Siep
--
Posted via http://www.ruby-....

4 Answers

matt

5/9/2008 2:19:00 AM

0

Siep Korteling <s.korteling@gmail.com> wrote:

> Hash.new(0) and the block form of Hash.new{_smart_stuff_} come in
> really handy while reading in a hash.
> When the reading is done however, my hash serves to provide data. At
> that stage, the default_value behaviour gets in the way. For instance,
> if the hash contains the number of goals for soccerplayers, I don't want
> the hash to return value "0" for the key "Wolfgang Amadeus Mozart"; by
> then I want plain old Nil, or "No data available".
>
> failed experiment:
>
> Class Hash
> def default_proc
> Nil
> end
> end
>
> How can Hash.new behaviour be changed?
>
> regards,
> Siep

h = Hash.new.merge(h)

So for example:

h = Hash.new(0)
h[:a] += 2
h[:a] += 1
h[:b] += 4
p h #=> {:a=>3, :b=>4}

h = Hash.new.merge(h)
p h[:c] #=> nil, it's now returning plain old nil

m.
--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Craig Demyanovich

5/9/2008 2:28:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Reset the default.

>> a = Hash.new(0)
=> {}
>> a["joe"]
=> 0
>> a.default = nil
=> nil
>> a["joe"]
=> nil

Regards,
Craig

Andrea Fazzi

5/9/2008 9:09:00 AM

0

Siep Korteling wrote:
> Hash.new(0) and the block form of Hash.new{_smart_stuff_} come in
> really handy while reading in a hash.
> When the reading is done however, my hash serves to provide data. At
> that stage, the default_value behaviour gets in the way. For instance,
> if the hash contains the number of goals for soccerplayers, I don't want
> the hash to return value "0" for the key "Wolfgang Amadeus Mozart"; by
> then I want plain old Nil, or "No data available".
>
> failed experiment:
>
> Class Hash
> def default_proc
> Nil
> end
> end
>
> How can Hash.new behaviour be changed?
>
> regards,
> Siep

a = Hash.new(0)

# do something with a

a = Hash.new { |h,k| h[k] = "No data available" }.merge(a)

But I don't know if this is what you want.

Bye.
Andrea
--
Posted via http://www.ruby-....

Siep Korteling

5/9/2008 11:42:00 PM

0

Craig Demyanovich wrote:
> Reset the default.
>
>>> a = Hash.new(0)
> => {}
>>> a["joe"]
> => 0
>>> a.default = nil
> => nil
>>> a["joe"]
> => nil
>
> Regards,
> Craig

Thanks all. When i'm stuck, the solution is almost always easier then I
imagined.

Regards,

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