Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUBY

ruby hash transform values

# Map key/values to array
cities = { sydney: "australia", london: "england" }
cities.map { |k,v| "#{k} is in #{v}" }
# ["sydney is in australia", "london is in england"]

# Alter all values in hash
score = {a: 1, b: 2, c: 3}
score.transform_values {|v| v*10 }
# => {:a=>10, :b=>20, :c=>30}

# Alter all keys in hash
camels = { Camel: 'A Camel', OneCamel: 'A one hump camel', TwoManyCamels: 'A two hump camel' }
# {:Camel=>"A Camel", :OneCamel=>"A one hump camel", :TwoManyCamels=>"A two hump camel"}
camels.transform_keys {|key| key.to_s.underscore.to_sym }
# {:camel=>"A Camel", :one_camel=>"A one hump camel", :two_many_camels=>"A two hump camel"}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #ruby #hash #transform #values
ADD COMMENT
Topic
Name
2+4 =