开发者

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

开发者 https://www.devze.com 2023-02-09 17:32 出处:网络
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?

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

精彩评论

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