Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/unicode/display_width.rb

Instance Method Summary collapse

Instance Method Details

#display_width(ambiguous = 1) ⇒ Object Also known as: display_size, display_length



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/unicode/display_width.rb', line 30

def display_width(ambiguous = 1)
  unpack('U*').inject(0){ |a,c|
    width = case Unicode::DisplayWidth.codepoint(c).to_s
            when *%w[F W]
              2
            when *%w[N Na H]
              1
            when *%w[A]
              ambiguous
            else
              1
            end
    a + width
  }
end