Class: String

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

Instance Method Summary collapse

Instance Method Details

#contains_cjk?Boolean

test if string contains east Asian character (RUBY_VERSION > 1.9)

Returns:

  • (Boolean)


148
149
150
# File 'lib/tabulate.rb', line 148

def contains_cjk?               # Oniguruma regex !!!
    (self =~ /\p{Han}|\p{Katakana}|\p{Hiragana}\p{Hangul}/)
end

#widthObject

actual string width



153
154
155
156
157
158
159
160
161
162
# File 'lib/tabulate.rb', line 153

def width
    if RUBY_VERSION >= '1.9'
        gsub(/(\e|\033|\33)\[[;0-9]*\D/,'').split(//).inject( 0 ) do |s, i|
            s += i.contains_cjk? ? 2 : 1
            s 
        end
    else
        gsub(/(\e|\033|\33)\[[;0-9]*\D/,'').size
    end
end