add Ruby 1.8.7 compatibility

I stole the code from here http://stackoverflow.com/a/4718414/104959