Ruby on Rails
PhpArray_change_key_case

As best I can tell, there’s no one function do do this one a ruby Hash. But here’s one way to do it.


dest = {}
src.each_key { |key|
  dest[key.upcase] = src[key]
}

Change ‘upcase’ to ‘downcase’ to get the lowercase equivalent.

Here’s an addition to Hash will handle altering keys:


class Hash
  def alter_keys(&filter)
    dest = {}
    each_key { |key|
      dest[filter.call(key)] = self[key]
    }
    dest
  end
end

With this function you can now do this:


a = {"keyA" => 1, "keyB" => 2}
a.alter_keys { |i| i.upcase } # => {"KEYA" ...
a.alter_keys { |i| i.downcase } # => {"keya" ...