[HELP] modifying hash

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,

···

--
Bil
http://fun3d.larc.nasa.gov

Hi --

···

On Wed, 21 Sep 2005, Bil Kleb wrote:

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

--
David A. Black
dblack@wobblini.net

Bil Kleb wrote:

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,

···

--
Bil
http://fun3d.larc.nasa.gov

What about
  hash.each{ |k,v| v.delete_if{|e| e==:a} }
?

···

On Wed, 21 Sep 2005 20:51:40 +0900 Bil Kleb <Bil.Kleb@NASA.gov> wrote:

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

--

"not using Ruby is punishment enough"
  -- James Britt, 8.5.2005, ruby-talk@ruby-lang.org

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.

···

--
Nobu Nakada