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:

  1. a comma separated string values, e.g. "2,3,1"
  2. 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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -