Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Jan 26 ’12

Using Enumerable::inject to modify a hash

Ever wanted to modify the keys and values of a Hash? Enumerable::inject has you covered.

Try this snippet from Stack Overflow:

my_hash = { a: 'foo', b: 'bar' }
# => {:a=>"foo", :b=>"bar"}
a_new_hash = my_hash.inject({}) { |h, (k, v)| h[k.upcase] = v.upcase; h }
# => {:A=>"FOO", :B=>"BAR"}

9 notes 0 comments

  1. haru012 reblogged this from rubyquicktips
  2. rubyloveinfo reblogged this from rubyquicktips
  3. davefp submitted this to rubyquicktips

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus