tags:

views:

52

answers:

2

hi!

I have a hash, each value is an array.

I want to build a new array containing the size of each value/array.

Example:

the hash

{"A"=>["1", "2", "3"], "B"=>["b", "toto"]}

the result

[3, 2]

thanks for your help

+2  A: 

I would do:

h.collect{|v|v[1].size}
Ben
+3  A: 
some_hash.values.map { |v| v.size }

and in 1.9, I believe you can do:

some_hash.values.map(&:size)
Alex Baranosky
works fine thanks
denisjacquemin
I think you can skip values and just do `some_hash.map {|k,v| v.size }`
kejadlen
@kejadlen: yeah I think I like your syntax the best of all
Alex Baranosky
Jörg W Mittag
For clarity, I think `hash.values.map` is more appropriate. It's possible that `hash.map` is faster, but I do not think it is clearer. Unless this is a core (frequently executed) function, clear should trump fast.
Myrddin Emrys