简体   繁体   中英

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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM