UTF-8 string in Ruby
I have the following module:
# encoding: utf-8
module RandomNameModule
def self.doesNothing(word)
str = ""
word.codepoints{|val|
str << val.chr
}
return str
end
end
and the following test:
# encoding: utf-8
require 'test/unit'
require '../src/RandomNameModule.rb'
class RandomNameTests < Test::Unit::TestCase
def testDoesNothing
sor开发者_StackOverflow中文版ted = WordSort.word_sort("£$&")
assert_equal("£$&", sorted)
end
end
When I run the test I get an assertion failure:
<"£$&"> expected but was
<"\xA3$&">.
This is because "£".codepoints{|x| x.chr}
returns the value \xA3
how can I make this return £
The Integer#chr
method used in your example seems to default to ASCII if you don't explicitely tell it what encoding to use:
def self.doesNothing(word)
str = ""
word.codepoints { |val| str << val.chr("utf-8") }
str
end
Also, using String#each_char
instead of String#codepoints
works fine as well:
def self.doesNothing(word)
str = ""
word.each_char { |val| str << val }
str
end
精彩评论