How to get the size of Chinese characters on Ruby

We usually get the length of a string by length method. But it is not the correct result of the string which composites of chinese charaters because the chinese charaters will take 2 space for each. On the other side, one more method bytesize of String, will return the3, the bytes length of the string.

1
2
"abc123){*~".bytesize #=>10
"あああ".bytesize #=>9

So, I define a function to get the result I want:

1
2
3
def kanjisize(string)
sring.each_char.map{|c| c.bytesize == 1 ? 1 : 2}.reduce(0, &:+)
end

and make it as a monkey patch of String with the path of lib/core_extensions/string/kanji_size.rb:

1
2
3
4
5
6
7
8
9
10
11
module CoreExtensions
module String
module KanjiSize
def kanjisize
self.each_char.map{|c| c.bytesize == 1 ? 1 : 2}.reduce(0, &:+)
end
end
end
end

String.send(:include, CoreExtensions::String::KanjiSize)
1
2
"abc123){*~".bytesize #=>10
"あああ".bytesize #=>6