dan
dan

Reputation: 45622

How can I make a #gsub call work with encodings in both Ruby 1.8 and Ruby 1.9?

I have the following code which requires Ruby 1.9, and I need to achieve the same functionality in Ruby 1.8. How can I accomplish this?

  # encoding: UTF-8
  ... [code omitted]
  body.force_encoding("UTF-8")
  body = body.gsub(/^(?=>)/, ">").gsub(/^(?!>)/, "> ")

body is a string obtained from an external source.

I think what I need is called a "shim" but I'm not sure.

Upvotes: 0

Views: 191

Answers (1)

the Tin Man
the Tin Man

Reputation: 160551

James Gray wrote a series of articles about dealing with encodings in Ruby. They're very good reading.

For 1.8.7 the jcode library can help.

$KCODE = "U"
require 'jcode'

Upvotes: 2

Related Questions