Ruby 1.8.7 String#chars compatibility problem with rails
Recently, I started getting weird "LoadError" with my Rails app on Dreamhost. I had an "Article" model which contained many "Photo" models. When the "Article" controller tried to refer to the photos in its object, I got the following error:
LoadError: Expected {full server path}/photo.rb to define Photo.
I had a photo.rb file which had class Photo defined. I was using attachment_fu plugin to implement photos. After some head scratching, I found that the error was in photo.rb file. Hence photo.rb was not loading correctly, and therefore a load error.
The actual error happened coz the Dreamhost guys had updated Ruby to 1.8.7 which has some incompatibility with Rails 2.0.2 that I was using. When I tried to create a Photo object on the command line, I got this error:
NoMethodError: undefined method '[]' for #<Enumerable:enumerator></code>
Ruby 1.8.7 has String#chars. This returns an Enumerator object but Rails 2.0.2 expects an ActiveSupport::MultiBye::Chars object.
We can avoid this conflict, by putting the following code in the config/initializers/ directory.
unless '1.9'.respond_to?(:force_encoding) String.class_eval do begin remove_method :chars rescue NameError # OK end end end
