使用 Ruby 1.8.7 或 1.9.2 进行编码

发布于 2024-09-25 23:50:37 字数 764 浏览 4 评论 0原文

我一直在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼痛 2024-10-02 23:50:37

恐怕我不明白你的问题。您的源代码文件有问题吗?我已经在控制台和 ruby​​ 脚本(1.8.7)中尝试过,它确实有效。

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/

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/]

and the output works as expected

5
5
ël

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/

梦罢 2024-10-02 23:50:37

这无需 C 扩展即可工作,并且在 1.8/1.9 上,并非所有字符串方法都工作(但它们很容易添加)

https:/ /github.com/grosser/string19

require 'rubygems'
require 'string19'
String19('hëllö').length == 5

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

require 'rubygems'
require 'string19'
String19('hëllö').length == 5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文