在Ruby 1.8.7或1.9.2中编码 [英] Encoding in Ruby 1.8.7 or 1.9.2

查看:148
本文介绍了在Ruby 1.8.7或1.9.2中编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用gem'字符编码,它不是建立在1.9.2,但是它在1.8.7,但即使我需要'encoding / character / utf-8'我仍然不能最简单的编码。

I have been trying to use the gem 'character-encodings' which doesn't build in 1.9.2 however it does in 1.8.7 but even when I require 'encoding/character/utf-8' I still cant do the simplest of encoding.

require 'encoding/character/utf-8'
str = u"hëllö"
str.length
  #=> 5
str.reverse.length
  #=> 5
str[/ël/]
  #=> "ël"

我得到

ruby-1.8.7-p302 >   # encoding: utf-8
ruby-1.8.7-p302 >   require 'encoding/character/utf-8'
 => nil 
ruby-1.8.7-p302 > str = u"hll"
 => u"hll" 
ruby-1.8.7-p302 > str.length
 => 3 
ruby-1.8.7-p302 >   #=> 5
ruby-1.8.7-p302 >   str.reverse.length
 => 3 
ruby-1.8.7-p302 >   #=> 5
ruby-1.8.7-p302 >   str[/l/]
 => "l" 

我的问题是,是否有一个非常不错的编码库,可以接受分配或可能不同的字符在那里。或者可以使用utf-16?我试过魔术代码#encoding:utf-8,似乎也没有做到。
谢谢

My question is, is there a really nice encoding library that can accept allot or possibly all the different characters out there. Or maybe use utf-16? I have tried the magic code "# encoding: utf-8" which didn't seem to do it either. Thank you

推荐答案

恐怕我不明白你的问题。您是否遇到源代码文件的问题?我已经在控制台和一个ruby脚本(1.8.7)尝试了它,并且它的工作。

I'm afraid I don't understand your question. Are you having issues with the source code file? I've tried it both in console and a ruby script (1.8.7), and it does work.

require 'rubygems'
require 'encoding/character/utf-8'
str = u'hëllö'
puts str.length
puts str.reverse.length
puts str[/ël/]

并且输出按预期工作

5
5
ël

在Ruby 1.9+(我在1.9.2预览中测试),你不需要一个库,因为标准库支持编码。 查看此发布更多信息
http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/

In Ruby 1.9+ (I tested in 1.9.2 preview) you don't need a library, as encoding is supported by the standard library. See this post for more information about it. http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/

这篇关于在Ruby 1.8.7或1.9.2中编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆