`
googya
  • 浏览: 140395 次
  • 性别: Icon_minigender_1
  • 来自: 汉川
社区版块
存档分类
最新评论

ruby中的字符编码

阅读更多
    长期以来,都被Ruby中的字符乱码所困惑,但是一直高估了解决乱码出现的难度,所以一直不敢去真正的理解,导致问题一直存在。看来逃避不是办法,要勇敢的去面对,是解决该问题的时候了!
    最近在看Leonard ricbardson &sam ruby合著的《RESTful web services》,很赞同书里面的观点。同时这本书同时给了我很多的启发,如何去了解以及使用那些提供了API的网站,虽然现在只能做些很小的客户端,仍然很有成就感。毕竟从不懂,到会用,到可以编写简单的客户端进步还是有点的。
    言归正传。
    看到 Ruby:rest-open-uri and net/http这一节的时候,看到书中举了一个例子
def print_my_recent_bookmarks(username,password)
  response=open('http://api.del.icio.us/v1/posts/recent',
    :http_basic_authentication=>[username,password])
  xml=response.read
  puts xml
  document=REXML::Document.new(xml)
  REXML::XPath.each(document,"/posts/post") do |e|
    puts "#{e.attributes['description']}:#{e.attributes['href']}"
  end
end
#username,password=ARGV
#unless username and password
#  puts "USAGE:#{$0}[username][password]"
#  exit
#end
print_my_recent_bookmarks(username,password)

例子当然是很简单的,就是返回最近在del.icio.us所提交的书签具体的API可以参见
http://delicious.com/help/api#posts_recent
返回的结果是出现了一堆乱码


鏈€杩戜娇鐢ㄧ殑涔︾:place:folder=BOOKMARKS_MENU&folder=UNFILED_BOOKMARKS&folder=TOOLBAR&sort=12&excludeQueries=1&excludeItemIfParentHasAnnotation=livemark%2FfeedURI&maxResults=10&queryType=1
鏃犳爣棰樻枃妗?file:///C:/Documents%20and%20Settings/Administrator/%E6%A1%8C%E9%9D%A2/xx.html
鑾峰彇涔︾闄勫姞杞欢:http://zh-cn.add-ons.mozilla.com/zh-CN/firefox/bookmarks/
甯姪鍜屾暀绋?http://zh-cn.www.mozilla.com/zh-CN/firefox/help/
鍏充簬鎴戜滑:http://zh-cn.www.mozilla.com/zh-CN/firefox/about/
鑷畾涔?Firefox:http://zh-cn.www.mozilla.com/zh-CN/firefox/customize/
鏂版墜涓婅矾:http://zh-cn.www.mozilla.com/zh-CN/firefox/central/
鎴戜篃瑕佸弬涓?http://zh-cn.www.mozilla.com/zh-CN/firefox/community/
鑾峰彇涔︾闄勫姞椤?https://zh-cn.add-ons.mozilla.com/zh-CN/firefox/bookmarks/

很郁闷吖!

很显然,这是字符集解析出现了问题,然后我想,问题会不会出现在服务器那端,结果一看那边是utf-8,这应该不会有问题了,那么问题是出在客户端这一边了。出现乱码是因为我这边没有按照utf-8进行解析,通常是按照gbk来的,当然就会出现乱码了,服务器和客户端用的不是同一种语言嘛,那不出问题才怪呢!

问题清楚了,如何解决?到http://www.ruby-doc.org/core/上找找,据说是要用iconv这个核心类其中的
   Iconv.iconv(to, from, *strs)

Shorthand for

  Iconv.open(to, from) { |cd|
    (strs + [nil]).collect { |s| cd.iconv(s) }
  }
这个方法。我也懒得自己写了,到javaeye上搜搜,我想这样的问题一定有人遇到过,那么一定有人把问题解决了,结果还真是看到了hooopo老兄早已注意到这个问题,还写了一篇 UTF8编码和正则表达式
我就直接把他的代码用上了
# To change this template, choose Tools | Templates
# and open the template in the editor.

#puts "Hello World"

require "open-uri"
require "rexml/document"
$KCODE='UTF-8'
require'iconv'
class String
 def to_gbk
  Iconv.iconv("GBK//IGNORE","UTF-8//IGNORE",self).to_s
 end
 def to_utf8
  Iconv.iconv("UTF-8//IGNORE","GBK//IGNORE",self).to_s
 end
end


def print_my_recent_bookmarks(username,password)
  response=open('http://api.del.icio.us/v1/posts/recent',
    :http_basic_authentication=>[username,password])
  xml=response.read
  #puts xml
  document=REXML::Document.new(xml)
  REXML::XPath.each(document,"/posts/post") do |e|
    puts "#{e.attributes['description']}".to_gbk+":"+"#{e.attributes['href']}".to_gbk
  end
end
#username,password=ARGV
#unless username and password
#  puts "USAGE:#{$0}[username][password]"
#  exit
#end
print_my_recent_bookmarks(username,password)



乱码就没有了,看得懂的东西出现了
About data mining in java:http://www.jdmp.org/
The Positive Legacy of C++ and Java:http://www.artima.com/weblogs/viewpost.jsp?thread=252441
彩字秀(czxiu.com) 在线生成彩字闪字趣味图片 QQ自定义表情 手机图片 非主流图片:http://www.czxiu.com/
Lifehacker, tips and downloads for getting things done:http://lifehacker.com/
最新头条:http://zh-cn.fxfeeds.mozilla.com/zh-CN/firefox/headlines.xml
访问最多:place:sort=8&maxResults=10
最近使用的书签:place:folder=BOOKMARKS_MENU&folder=UNFILED_BOOKMARKS&folder=TOOLBAR&sort=12&excludeQueries=1&excludeItemIfParentHasAnnotation=livemark%2FfeedURI&maxResults=10&queryType=1
无标题文档:file:///C:/Documents%20and%20Settings/Administrator/%E6%A1%8C%E9%9D%A2/xx.html
获取书签附加软件:http://zh-cn.add-ons.mozilla.com/zh-CN/firefox/bookmarks/
帮助和教程:http://zh-cn.www.mozilla.com/zh-CN/firefox/help/
关于我们:http://zh-cn.www.mozilla.com/zh-CN/firefox/about/


我敢肯定,这出现了仅仅是字符问题中很小的一个,而且是很好解决的一个,但是我起码有信心了,遇到关于字符的问题不会再逃避了,关键是逃避也没有用!既然逃避没用,那就消灭它们!
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics