Search
 
SCRIPT & CODE EXAMPLE
 

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"}
Comment

ruby hash transform keys

hash = { name: 'Rob', age: '28' }

hash.transform_keys{ |key| key.to_s.upcase }
# => { "NAME" => "Rob", "AGE" => "28" }
Comment

PREVIOUS NEXT
Code Example
Ruby :: travis ci NameError: uninitialized constant SimpleCov 
Ruby :: integer to string ruby 
Ruby :: hello world in ruby 
Ruby :: how to create migration with uniqueness inrails 
Ruby :: ruby get file extension 
Ruby :: rspec add support folder 
Ruby :: rails get source method 
Ruby :: substring replace in ruby 
Ruby :: ruby list all class methods 
Ruby :: require relative ruby 
Ruby :: table name from rails console 
Ruby :: rails order 
Ruby :: autoload lib directory rails 
Ruby :: ruby create csv 
Ruby :: ruby downcase 
Ruby :: create a new hash from existing hash ruby 
Ruby :: ruby each do method 
Ruby :: ruby hash delete 
Ruby :: run a rake task 
Ruby :: ruby routes 
Ruby :: generate dates using period rails 
Ruby :: ruby join hash to string 
Ruby :: ruby array last 
Ruby :: ! in ruby 
Ruby :: important topic on ruby 
Ruby :: run bundle without production in rails 
Ruby :: rails deliver_later with delay 
Ruby :: class inheriting multiple modules in ruby 
Ruby :: time loop start with non zero in ruby 
Ruby :: get specific key value from array of hashes in ruby 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =