This example is taken directly from the Ruby 2.4.1 documentation, and I can confirm I am running 2.4.1:
({a: 1, b: 2, c: 3}).transform_keys {|k| k.to_s}
When I execute it, I receive the following error:
NoMethodError: undefined method `transform_keys' for {:a=>1, :b=>2, :c=>3}:Hash
Why is the transform_keys
method not defined?
As observed in another question, it appears that http://ruby-doc.org currently (erroneously) generates the documentation for Ruby 2.4.1 based on Ruby trunk instead of the actually released 2.4.1 version.
Unfortunately, the Hash#transform_keys
method is not yet released as part of any 2.4 release. It was developed and comitted to Ruby trunk with Feature #13583 but was not (yet) backported to the stable 2.4 branch.
As a workaround for that, you can use this method instead:
def transform_keys(hash)
result = {}
hash.each_pair do |key, value|
result[yield(key)] = value
end
result
end
Equivalently (that is: a bit shorter but also a slightly slower) you could use this:
def transform_keys(hash)
hash.keys.each_with_object({}) do |key, result|
result[yield(key)] = hash[key]
end
end
If you are bold, you can add this as a core-patch to the Hash
class where you then just have to replace every mention of hash
with self
.
Note that ActiveSupport (i.e. Rails) brings a core-patch with this exact method since about forever. They use a mixture of both implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With