[HELP] modifying hash

B

Bil Kleb

I've got a mental block on refactoring the following,

# given a hash of arrays,
hash = { 1=>[:a,:b,:c], 2=>[:d,:e], 3=>[:a,:c] }

# remove a specified element from the arrays if present
hash.each{ |k,v| hash[k] = v.delete_if{|e| e==:a} }

=> { 1=>[:b,:c], 2=>[:d,:e], 3=>[:c] }

Isn't there a simpler way to remove the matching
elements? (Maybe my data structure is just lame?)

Thanks,
 
B

Bil Kleb

Bil said:
I've got a mental block on refactoring the following,

hash.each{ |k,v| hash[k] = v.delete_if{|e| e==:a} }

Broke the block?

hash.each_value{ |v| v.delete :a }

Later,
 
D

David A. Black

Hi --

I've got a mental block on refactoring the following,

# given a hash of arrays,
hash = { 1=>[:a,:b,:c], 2=>[:d,:e], 3=>[:a,:c] }

# remove a specified element from the arrays if present
hash.each{ |k,v| hash[k] = v.delete_if{|e| e==:a} }

=> { 1=>[:b,:c], 2=>[:d,:e], 3=>[:c] }

Isn't there a simpler way to remove the matching
elements? (Maybe my data structure is just lame?)

Slightly simpler:

hash.values.each {|v| v.delete:)a) }


David
 
N

nobu.nokada

Hi,

At Wed, 21 Sep 2005 21:01:52 +0900,
David A. Black wrote in [ruby-talk:156926]:
hash.values.each {|v| v.delete:)a) }

Hash#each_value would be better.
 

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

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,517
Latest member
TashaLzw39

Latest Threads

Top