I’ve got a gem that’s used a bunch of people using a bunch of different Ruby interpreters, and it includes what boils down to this code:
res = RestClient.post(...)
doc = REXML::Document.new(res).root
The content of res is always UTF-8, and this works fine in Ruby 1.8, but it blows up under Ruby 1.9 if the response is not pure ASCII and the user’s default encoding is not UTF-8.
Now, if I wanted to make this work on Ruby 1.9 alone, I’d just stick res.force_encoding('utf-8') in there and be done with it, but that method is 1.9-only and then breaks under Ruby 1.8:
NoMethodError: undefined method `force_encoding' for #<String:0x101318178>
The best solution can come up with is this, which forces the systemwide default encoding to UTF-8:
Encoding.default_external = 'UTF-8' if defined? Encoding
Better ideas, or is this as good as it gets? Will there be any negative impact on library users who are trying to use different encodings?
I’m with Mike Lewis in using
respond_to, but don’t do it on the variable res everywhere throughout your code.I took a look at your code in gateway.rb and it looks like everywhere you are using
res, it gets set by a call tomake_api_requestso you could add this before your return statement in that method:Even if it’s other places but it’s not literally with every string you encounter, I’m sure you can find a way to refactor the code that makes sense and solves the problems in one place instead of everywhere you encounter it.
Are you having a problem with other places?