backport to ruby 1.8.7

Kazuki - here you go!