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.
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}%"] } ]
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.
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 replace all nil value with in a ruby hash recursively?
Here is a recursive method that does not change the original hash.
Code
def denilize(h)
h.each_with_object({}) { |(k,v),g|
g[k] = (Hash === v) ? denilize(v) : v.nil? ? '' : v }
end
Examples
h = { "a"=>{ "b"=>{ "c"=>nil } } }
denilize(h) #=> { "a"=>{ "b"=>{ "c"=>"" } } }
h = { "a"=>{ "b"=>{ "c"=>nil , "d"=>3, "e"=>nil}, "f"=>nil } }
denilize(h) #=> { "a"=>{ "b"=>{ "c"=>"" , "d"=>3, "e"=>""}, "f"=>"" } }
Ruby / Replace value in array of hash
Another way, using find
1.9.3p194 :007 > array1 = [{"name"=>"Bob"}, {"age"=>"30"}]
=> [{"name"=>"Bob"}, {"age"=>"30"}]
1.9.3p194 :008 > hash1 = array1.find { |h| h['age'] == "30" }
=> {"age"=>"30"}
1.9.3p194 :009 > hash1['age'] = 31
=> 31
1.9.3p194 :010 > array1
=> [{"name"=>"Bob"}, {"age"=>31}]
Related Topics
Rails 3 Install Error: "Invalid Value for @Cert_Chain"
How to Create a Hash in Ruby That Compares Strings, Ignoring Case
301 Moved Permanently After S3 Uploading
Ruby Minitest: Suite- or Class- Level Setup
Detect If Application Was Started as Http Server or Not (Rake Task, Rconsole etc)
Travis Reports Odd Message of Corrupted Gemfile.Lock
Rails Active Admin CSS Conflicting with Twitter Bootstrap CSS
Windows/Ruby/Rails Install --- .Cannot Load Such File -- SQLite3/Sqlite3_Native Windows
Jekyll Serve Dependency Error - Could Not Open 'Lib Curl'
In Ruby Why Does Nil.Id Return 4
Ruby: What Is the Easiest Way to Remove the First Element from an Array
Connect to a Locally Built Jekyll Server Using Mobile Devices in the Lan