使用 Ruby 1.8.7 或 1.9.2 进行编码
我一直在尝试使用 gem 'character-encodings',它不在 1.9.2 中构建,但在 1.8.7 中构建,但即使我需要 'encoding/character/utf-8' 我仍然无法做最简单的编码。
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”,但它似乎也不起作用。 谢谢
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"
I get
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"
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕我不明白你的问题。您的源代码文件有问题吗?我已经在控制台和 ruby 脚本(1.8.7)中尝试过,它确实有效。
并且输出按预期工作
在 Ruby 1.9+(我在 1.9.2 预览版中测试)中,您不需要库,因为标准库支持编码。 查看此内容发布有关它的更多信息。
http: //yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/
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.
and the output works as expected
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/
这无需 C 扩展即可工作,并且在 1.8/1.9 上,并非所有字符串方法都工作(但它们很容易添加)
https:/ /github.com/grosser/string19
this works without c extensions and on 1.8/1.9, not all string methods work(but they are easy to add)
https://github.com/grosser/string19