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 :: how to down a particular migration in rails 
Ruby :: ruby generate random number 
Ruby :: ruby array has element 
Ruby :: max keys from hash ruby 
Ruby :: remove first element from an array ruby 
Ruby :: how to force exit server in rails 
Ruby :: rails string to date 
Ruby :: how to create a database in production mode rails 
Ruby :: rails order 
Ruby :: conditional operator in ruby 
Ruby :: how to write CSV file in rails 
Ruby :: ruby pop array 
Ruby :: how to remove nested array brackets ruby 
Ruby :: ruby is character 
Ruby :: rails logger stdout 
Ruby :: rails generate controller no respec 
Ruby :: rails keep all params except for some 
Ruby :: rails change resource name 
Ruby :: grails 3 cron jobs 
Ruby :: ruby on rails binding.pry 
Ruby :: ruby if statement multiple conditions 
Ruby :: resources rails 
Ruby :: rails migration column types 
Ruby :: activerecord exclude 
Ruby :: rails interrupt if tooo long 
Ruby :: ruby mine show all blocks 
Ruby :: rails migration error 
Ruby :: elsif ruby 
Ruby :: ruby read file line by line 
R :: select columns without na in r 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =