Ruby: What is the easiest method to update Hash values?
You can use update
(alias of merge!
) to update each value using a block:
hash.update(hash) { |key, value| value * 2 }
Note that we're effectively merging hash
with itself. This is needed because Ruby will call the block to resolve the merge for any keys that collide, setting the value with the return value of the block.
How to change Hash values?
my_hash.each { |k, v| my_hash[k] = v.upcase }
or, if you'd prefer to do it non-destructively, and return a new hash instead of modifying my_hash
:
a_new_hash = my_hash.inject({}) { |h, (k, v)| h[k] = v.upcase; h }
This last version has the added benefit that you could transform the keys too.
How can I replace a single Hash Value in Ruby by using its number
You can do hashOne[hashOne.keys[3]] = 'rubypro'
. Also I remember reading that Hashes in ruby are ordered but I'm not entirely sure. (Be careful with using indices because they may not be ordered in other languages) I would ensure that the keys are always returned in the same order before trying to assign a key with an index.
If you really wanted to use an index, I'd recommend using an array because that's not the purpose of a key/value hash.
Efficient way to update values to array of hashes in ruby?
You could just use the same object:
item_1 = {'id' => 1, 'cost' => '2.00'}
item_2 = {'id' => 2, 'cost' => '6.00'}
items = [item_1, item_2, item_1, item_1, item_1]
#=> [{"id"=>1, "cost"=>"2.00"}, {"id"=>2, "cost"=>"6.00"},
# {"id"=>1, "cost"=>"2.00"}, {"id"=>1, "cost"=>"2.00"},
# {"id"=>1, "cost"=>"2.00"}]
This makes updates trivial:
item_1['cost'] = '8.00'
items
#=> [{"id"=>1, "cost"=>"8.00"}, {"id"=>2, "cost"=>"6.00"},
# {"id"=>1, "cost"=>"8.00"}, {"id"=>1, "cost"=>"8.00"},
# {"id"=>1, "cost"=>"8.00"}]
Update certain key in hash
Just update a value for date_1 and date_2:
hash['date_1'] = hash['date_1'].to_date if hash['date_1']
hash['date_2'] = hash['date_2'].to_date if hash['date_2']
or you can do it with each
if you have more a date keys:
date_keys = [:date_1, :date_2]
hash.each do |k,v|
hash[k] = v.to_date if date_keys.include?(k)
end
Updating Ruby Hash Values with Array Values
I would just do:
h.keys.zip(@data).to_h
If the only purpose of h
is as an interim step getting to the result, you can dispense with it and do:
columns.zip(@data).to_h
Changing every value in a hash in Ruby
If you want the actual strings themselves to mutate in place (possibly and desirably affecting other references to the same string objects):
# Two ways to achieve the same result (any Ruby version)
my_hash.each{ |_,str| str.gsub! /^|$/, '%' }
my_hash.each{ |_,str| str.replace "%#{str}%" }
If you want the hash to change in place, but you don't want to affect the strings (you want it to get new strings):
# Two ways to achieve the same result (any Ruby version)
my_hash.each{ |key,str| my_hash[key] = "%#{str}%" }
my_hash.inject(my_hash){ |h,(k,str)| h[k]="%#{str}%"; h }
If you want a new hash:
# Ruby 1.8.6+
new_hash = Hash[*my_hash.map{|k,str| [k,"%#{str}%"] }.flatten]
# Ruby 1.8.7+
new_hash = Hash[my_hash.map{|k,str| [k,"%#{str}%"] } ]
Related Topics
Order Products by Association Count
Handling Exceptions Raised in a Ruby Thread
How to Get a Particular Line from a File
How to Change Column Type in Heroku
How to Find Out Who Is Connected to Actioncable
Ruby Block to String Instead of Executing
How to Create a Nokogiri Case Insensitive Xpath Selector
How to Read a File from Bottom to Top in Ruby
Xpath Expression for Regex-Like Matching
Determining If a Variable Is Within Range
Connect to a Locally Built Jekyll Server Using Mobile Devices in the Lan
Given a Class, See If Instance Has Method (Ruby)