Just got some failing specs from our 1.8.6 test runner, when using OrderedHash#first, so we fix that…
# add {}.first if its not there (1.8.6) unless {}.respond_to?(:first) class Hash def first return if keys.empty? [keys.first, self[keys.first]] end end end
I ran into the same issue a while ago. Actually the method is missing on the Enumerable module and not only the Hash class, but it is still quite easy to implement a fully compliant back port. It also supports the optional length argument to fetch more the one element.
http://gist.github.com/458628
The code was inspired by JRuby’s implementation, but I cannot find the relevant lines any more. They might be gone.
I think Rubinius’ implementation is even more straight forward. It relies on take, which is also not part of 1.8.6, but can be easily added as well.
http://github.com/evanphx/rubinius/blob/master/kernel/common/enumerable.rb
looks interesting, but as long as this simple fix is enough for our needs i see no reason to upgrade to this ‘bigger-hack’ 🙂