As noted in another question , it seems that http://ruby-doc.org is currently (erroneously) generating documentation for Ruby 2.4.1 based on Ruby trunk instead of the actually released version 2.4.1.
Unfortunately, the Hash#transform_keys method has not yet been released as part of any version 2.4. It was developed and included in the Ruby trunk with Feature # 13583 , but was not (yet) included in the 2.4 stable branch.
As a workaround, you can use this method:
def transform_keys(hash) result = {} hash.each_pair do |key, value| result[yield(key)] = value end result end
Equivalent (i.e.: a little shorter, but also a bit slower), you cpuld use this:
def transform_keys(hash) hash.keys.each_with_object({}) do |key, result| result[(yield(key)] = hash[value] end end
If you are in bold, you can declare this as the main patch to the Hash class, where you just need to replace every mention of Hash with self .
Please note that ActiveSupport (i.e. Rails) brings the main patch using this exact method since forever. They use a mixture of both implementations.
source share