How to re-replace Hash default value behaviour?

Discussion in 'Ruby' started by Siep Korteling, May 9, 2008.

  1. 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/.
     
    Siep Korteling, May 9, 2008
    #1
    1. Advertising

  2. Siep Korteling

    matt neuburg Guest

    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


    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.tidbits.com/matt/
    Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
    AppleScript - http://www.amazon.com/gp/product/0596102119
    Read TidBITS! It's free and smart. http://www.tidbits.com
     
    matt neuburg, May 9, 2008
    #2
    1. Advertising

  3. [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
     
    Craig Demyanovich, May 9, 2008
    #3
  4. Siep Korteling

    Andrea Fazzi Guest

    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/.
     
    Andrea Fazzi, May 9, 2008
    #4
  5. 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/.
     
    Siep Korteling, May 10, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    539
    red floyd
    Nov 10, 2011
  2. Marcel Molina Jr.

    Hash hidden in hash with default object

    Marcel Molina Jr., Jun 25, 2005, in forum: Ruby
    Replies:
    3
    Views:
    121
    Marcel Molina Jr.
    Jun 25, 2005
  3. Une bévue
    Replies:
    5
    Views:
    153
    Une bévue
    Aug 10, 2006
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    627
    David A. Black
    Jul 2, 2008
  5. Johan Martinez
    Replies:
    3
    Views:
    293
    Johan Martinez
    May 22, 2011
Loading...

Share This Page