开发者

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")
  bo开发者_运维技巧dy = 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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜