Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to re-replace Hash default value behaviour?

Reply
Thread Tools

How to re-replace Hash default value behaviour?

 
 
Siep Korteling
Guest
Posts: n/a
 
      05-09-2008
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-forum.com/.

 
Reply With Quote
 
 
 
 
matt neuburg
Guest
Posts: n/a
 
      05-09-2008
Siep Korteling <(E-Mail Removed)> 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 = http://www.velocityreviews.com/forums/(E-Mail Removed), http://www.tidbits.com/matt/
Leopard - http://www.takecontrolbooks.com/leop...stomizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
 
Reply With Quote
 
 
 
 
Craig Demyanovich
Guest
Posts: n/a
 
      05-09-2008
[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

 
Reply With Quote
 
Andrea Fazzi
Guest
Posts: n/a
 
      05-09-2008
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-forum.com/.

 
Reply With Quote
 
Siep Korteling
Guest
Posts: n/a
 
      05-09-2008
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-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
hash key to var name of value hash key value Une bévue Ruby 5 08-10-2006 04:05 PM
Hash hidden in hash with default object Marcel Molina Jr. Ruby 3 06-25-2005 04:34 PM



Advertisments