ruby - Is there a one liner or more efficient way to get this done? -
given hash, example:
hash = { "fish" => 2, "cat" => 3, "dog" => 1 }
i need get:
- a comma separated string values, e.g. "2,3,1"
- an integer hold total sum of values, e.g. 6
my current code:
value_string = hash.map { |k,v| "#{v}"}.join(',') sum = 0 hash.map { |k,v| sum += v}
you can this:
hash.values.join(",") # => "2,3,1" hash.values.inject(:+) # => 6
Comments
Post a Comment