Skip to content

Instantly share code, notes, and snippets.

@chrisjpowers
Forked from mattpuchlerz/Hash#map_values.rb
Created March 25, 2009 00:21
Show Gist options
  • Save chrisjpowers/84454 to your computer and use it in GitHub Desktop.
Save chrisjpowers/84454 to your computer and use it in GitHub Desktop.
class Hash
def map_values
h = {}; self.each_pair { |k,v| h[k] = yield(v) }; h
end
end
[ 'asdf', 'qwer' ].map{ |val| val * 2 }
# => [ 'asdfasdf', 'qwerqwer' ]
{ :one => 'asdf', :two => 'qwer' }.map{ |val| val * 2 }
# => [ [:one, 'asdfasdf'], [:two, 'qwerqwer'] ]
{ :one => 'asdf', :two => 'qwer' }.map_values{ |val| val * 2 }
# => { :one => 'asdfasdf', :two => 'qwerqwer' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment