开发者

Using Ruby 1.9.1, how could I access this string's characters one at a time?

I don't know how else to explain, so I'll give you 开发者_运维百科List of greek words with english derivatives. Look at the a table, please, first column. Notice there are words like ἄβαξ. Using Ruby 1.9.1, which has better encoding support than Ruby 1.8, how could I iterate over each character forming that word? For example, I'd like to get the letters in order, one at a time, like:

ἄ
β
α
ξ

I'm not really sure how to go about that, as the .size method reports a different length of the string than the ones we perceive. Can you help?


Example:

#!/usr/bin/env ruby19

str = "ἄβαξ"
puts "#{str} - encoding: #{str.encoding.name} / size: #{str.size}"
str.each_char do |c|
  puts c
end

Using some Google-fu, you'll find a lot of good articles on Ruby 1.9 and character encoding.


The following seems to work in 1.9

testStr="ἄβαξ"
testStr.each_char { |k|
        puts k
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜