hash.clear("value")

Discussion in 'Ruby' started by Andrew Stone, Nov 28, 2007.

  1. Andrew Stone

    Andrew Stone Guest

    Note: parts of this message were removed by the gateway to make it a legal Usenet post.

    Hello all,

    Was just wondering if there was something simple like:

    hash.clear("value")

    This would remove all entries from the hash whose value == "value".

    Yeah, I know about delete_if, this would just be more succinct. I can be
    lazy sometimes.

    thanks,
    andy

    --
    Andrew Stone
     
    Andrew Stone, Nov 28, 2007
    #1
    1. Advertising

  2. Andrew Stone

    MonkeeSage Guest

    On Nov 28, 1:47 pm, Andrew Stone <> wrote:
    > Note: parts of this message were removed by the gateway to make it a legal Usenet post.
    >
    > Hello all,
    >
    > Was just wondering if there was something simple like:
    >
    > hash.clear("value")
    >
    > This would remove all entries from the hash whose value == "value".
    >
    > Yeah, I know about delete_if, this would just be more succinct. I can be
    > lazy sometimes.
    >
    > thanks,
    > andy
    >
    > --
    > Andrew Stone


    You can always wrap delete_if...

    class Hash
    def remove(target)
    self.delete_if { | key, value |
    value == target
    }
    end
    end

    Regards,
    Jordan
     
    MonkeeSage, Nov 28, 2007
    #2
    1. Advertising

  3. 2007/11/28, MonkeeSage <>:
    > On Nov 28, 1:47 pm, Andrew Stone <> wrote:
    > > Note: parts of this message were removed by the gateway to make it a legal Usenet post.
    > >
    > > Hello all,
    > >
    > > Was just wondering if there was something simple like:
    > >
    > > hash.clear("value")
    > >
    > > This would remove all entries from the hash whose value == "value".
    > >
    > > Yeah, I know about delete_if, this would just be more succinct. I can be
    > > lazy sometimes.
    > >
    > > thanks,
    > > andy
    > >
    > > --
    > > Andrew Stone

    >
    > You can always wrap delete_if...
    >
    > class Hash
    > def remove(target)
    > self.delete_if { | key, value |
    > value == target
    > }
    > end
    > end


    Just a subtle point: IMHO it is better to put "target" on the left
    side of the comparison because that gives the caller more control on
    the deletion criterion:

    class Hash
    def remove(target)
    delete_if { | key, value |
    target == value
    }
    end
    end

    For example, you could do

    crit = Object.new
    def crit.==(x) x < 10 end
    a_hash.remove crit # remove all entries where value is < 10

    It's a similar pattern to how "case" does comparison via "===". But
    then again, this is probably even better:

    class Hash
    def remove_val
    delete_if {|k,v| yield v}
    self
    end
    end

    Because it's more modular. But then again, you can use delete_if directly. :)

    Kind regards

    robert


    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Nov 29, 2007
    #3
  4. Andrew Stone

    MonkeeSage Guest

    On Nov 29, 4:35 am, Robert Klemme <> wrote:
    > 2007/11/28, MonkeeSage <>:
    >
    >
    >
    > > On Nov 28, 1:47 pm, Andrew Stone <> wrote:
    > > > Note: parts of this message were removed by the gateway to make it a legal Usenet post.

    >
    > > > Hello all,

    >
    > > > Was just wondering if there was something simple like:

    >
    > > > hash.clear("value")

    >
    > > > This would remove all entries from the hash whose value == "value".

    >
    > > > Yeah, I know about delete_if, this would just be more succinct. I can be
    > > > lazy sometimes.

    >
    > > > thanks,
    > > > andy

    >
    > > > --
    > > > Andrew Stone

    >
    > > You can always wrap delete_if...

    >
    > > class Hash
    > > def remove(target)
    > > self.delete_if { | key, value |
    > > value == target
    > > }
    > > end
    > > end

    >
    > Just a subtle point: IMHO it is better to put "target" on the left
    > side of the comparison because that gives the caller more control on
    > the deletion criterion:
    >
    > class Hash
    > def remove(target)
    > delete_if { | key, value |
    > target == value
    > }
    > end
    > end
    >
    > For example, you could do
    >
    > crit = Object.new
    > def crit.==(x) x < 10 end
    > a_hash.remove crit # remove all entries where value is < 10


    Nice. :)

    > It's a similar pattern to how "case" does comparison via "===". But
    > then again, this is probably even better:
    >
    > class Hash
    > def remove_val
    > delete_if {|k,v| yield v}
    > self
    > end
    > end
    >
    > Because it's more modular. But then again, you can use delete_if directly. :)


    Yes, but we're lazy! ;) No blocks here! Heh. Otherwise we would just
    use delete_if! ;)

    > Kind regards
    >
    > robert
    >
    > --
    > use.inject do |as, often| as.you_can - without end
     
    MonkeeSage, Nov 30, 2007
    #4
  5. 2007/11/30, MonkeeSage <>:
    > On Nov 29, 4:35 am, Robert Klemme <> wrote:
    > > 2007/11/28, MonkeeSage <>:
    > >
    > >
    > >
    > > > On Nov 28, 1:47 pm, Andrew Stone <> wrote:
    > > > > Note: parts of this message were removed by the gateway to make it a legal Usenet post.

    > >
    > > > > Hello all,

    > >
    > > > > Was just wondering if there was something simple like:

    > >
    > > > > hash.clear("value")

    > >
    > > > > This would remove all entries from the hash whose value == "value".

    > >
    > > > > Yeah, I know about delete_if, this would just be more succinct. I can be
    > > > > lazy sometimes.

    > >
    > > > > thanks,
    > > > > andy

    > >
    > > > > --
    > > > > Andrew Stone

    > >
    > > > You can always wrap delete_if...

    > >
    > > > class Hash
    > > > def remove(target)
    > > > self.delete_if { | key, value |
    > > > value == target
    > > > }
    > > > end
    > > > end

    > >
    > > Just a subtle point: IMHO it is better to put "target" on the left
    > > side of the comparison because that gives the caller more control on
    > > the deletion criterion:
    > >
    > > class Hash
    > > def remove(target)
    > > delete_if { | key, value |
    > > target == value
    > > }
    > > end
    > > end
    > >
    > > For example, you could do
    > >
    > > crit = Object.new
    > > def crit.==(x) x < 10 end
    > > a_hash.remove crit # remove all entries where value is < 10

    >
    > Nice. :)
    >
    > > It's a similar pattern to how "case" does comparison via "===". But
    > > then again, this is probably even better:
    > >
    > > class Hash
    > > def remove_val
    > > delete_if {|k,v| yield v}
    > > self
    > > end
    > > end
    > >
    > > Because it's more modular. But then again, you can use delete_if directly. :)

    >
    > Yes, but we're lazy! ;) No blocks here! Heh. Otherwise we would just
    > use delete_if! ;)


    Laziness is exactly the reason why I use delete_if. Why? Because I
    can be sure it's there. I don't have to create a lib of my personal
    extensions, require it in every script and make sure it can be found
    via RUBYLIB. Granted, setting RUBYLIB is a one time effort, but
    requiring needs to be done for every script plus I cannot pass scripts
    around as easily; I have to at least shop two files. etc.

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Nov 30, 2007
    #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. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    1,048
    Mark Fitzpatrick
    Jan 31, 2008
  2. InvalidLastName

    Unrecognized element 'add' after <clear></clear>

    InvalidLastName, Feb 26, 2007, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    980
    Steven Cheng[MSFT]
    Mar 6, 2007
  3. rp
    Replies:
    1
    Views:
    544
    red floyd
    Nov 10, 2011
  4. Une bévue
    Replies:
    5
    Views:
    153
    Une bévue
    Aug 10, 2006
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    630
    David A. Black
    Jul 2, 2008
Loading...

Share This Page