views:

1172

answers:

4

Hi!

I'd like to replace each value in a hash with value.some_method.

For example in a simple hash {"a" => "b", "c" => "d"} every value should be .upcase-d so it looks like {"a" => "B", "c" => "D"}.

I tried #collect and #map but always just get arrays back. Is there an 'elegant' way to do this?

Thanks in advance,

Adam Nonymous

UPDATE: Damn, I forgot: The hash is in an instance variable which should not be changed. I need a new hash with the changed values, but would prefer not to define that variable explicitly and then loop over the hash filling it. Something like new_hash = hash.magic {...} ;)

+11  A: 
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.

kch
That's it! Thanks very much for the astonishingly fast answer and update! :)
+3  A: 
h = {"a" => "b", "c" => "d"}
h.each{|i,j| j.upcase!} # now contains {"a" => "B", "c" => "D"}.
Chris Doggett
that's good, but it should be noted that it only works for when j has a destructive method that acts unto itself. So it can't handle the general value.some_method case.
kch
Good point, and with his update, your second solution (non-destructive) is the best.
Chris Doggett
A: 

I do something like this:

new_hash = Hash[*original_hash.collect{|key,value| [key,value + 1]}.flatten]

This provides you with the facilities to transform the key or value via any expression also (and it's non-destructive, of course).

This is quite clever too. Thanks! :)
A: 

You may want to go a step further and do this on a nested hash. Certainly this happens a fair amount with Rails projects.

Here's some code to ensure a params hash is in UTF-8:

  def convert_hash hash
    hash.inject({}) do |h,(k,v)|
      if v.kind_of? String
        h[k] = to_utf8(v) 
      else
        h[k] = convert_hash(v)
      end
      h
    end      
  end    

  # Iconv UTF-8 helper
  # Converts strings into valid UTF-8
  #
  # @param [String] untrusted_string the string to convert to UTF-8
  # @return [String] your string in UTF-8
  def to_utf8 untrusted_string=""
    ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
    ic.iconv(untrusted_string + ' ')[0..-2]
  end  
Tokumine