(ruby2.1.1)
間違えてたので修正
def char_bytesize_for(char) char.bytesize == 1 ? 1 : 2 end def sum_bytesize_for(text) text.each_char.map { |c| char_bytesize_for(c) }.inject(:+) end puts char_bytesize_for("日") #=> 2 puts sum_bytesize_for("日本語abcd") #=> 10
こんな感じ?