hash.clear("value")

A

Andrew Stone

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
 
M

MonkeeSage

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

You can always wrap delete_if...

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

Regards,
Jordan
 
R

Robert Klemme

2007/11/28 said:
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
 
M

MonkeeSage

2007/11/28, MonkeeSage <[email protected]>:






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! ;)
 
R

Robert Klemme

2007/11/30 said:
Nice. :)


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Setter method for Hash value 19
opposite .nil? 15
:: and . 6
Tasks 1
Looking for a Sudoku Algorithm to implement in Ruby 5
Building Ruby From Source? 6
Modules as namespaces 2
[ANN] siren 0.2.0 Released 0

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top