views:

156

answers:

1

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

+2  A: 

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/

Chubas
Thank you:) I feel a bit um yeah
Davinj