Class: BaseConvert::Chars

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

Overview

Chars provides ways of populating an ordered(as constructed) set with characters in UTF-8.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChars

Returns a new instance of Chars.



5
6
7
# File 'lib/base_convert/chars.rb', line 5

def initialize
  @start,@stop,@a = 32,126,[]
end

Instance Attribute Details

#startObject

Returns the value of attribute start.



9
10
11
# File 'lib/base_convert/chars.rb', line 9

def start
  @start
end

#stopObject

Returns the value of attribute stop.



9
10
11
# File 'lib/base_convert/chars.rb', line 9

def stop
  @stop
end

Instance Method Details

#add(x) ⇒ Object



47
# File 'lib/base_convert/chars.rb', line 47

def add(x)    = chars_in(x){@a.push _1 unless @a.include?_1}

#chars_in(x) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/base_convert/chars.rb', line 32

def chars_in(x)
  case x
  when Regexp
    (@start..@stop).each{x.match?(c=_1.chr(Encoding::UTF_8)) && yield(c)}
  when Symbol
    yield x[1..-1].to_i((x[0]=='u')? 16: 10).chr(Encoding::UTF_8)
  when String
    x.chars.each{|c| yield c}
  when Integer
    yield x.chr(Encoding::UTF_8)
  else
    raise "expected Regexp|Symbol|String|Integer, got #{x.class}"
  end
end

#remove(x) ⇒ Object



49
# File 'lib/base_convert/chars.rb', line 49

def remove(x) = chars_in(x){@a.delete _1}

#set(s) ⇒ Object

Chars can search a range in UTF-8 for character classes. Use Chars#set to set the start and stop of the range. i<n>: @start=n.to_i v<n>: @start=n.to_i(16) j<n>: @stop=n.to_i w<n>: @stop=n.to_i(16)



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/base_convert/chars.rb', line 19

def set(s)
  unless /^([ij]\d+)|([vw]\h+)$/.match?(s)
    raise 'expected /^([ij]\d+)|([vw]\h+)$/'
  end
  t,n = s[0],s[1..-1]
  case t
  when 'i','v'
    @start = n.to_i((t=='v')? 16 : 10)
  when 'j','w'
    @stop = n.to_i((t=='w')? 16 : 10)
  end
end

#to_aObject



10
# File 'lib/base_convert/chars.rb', line 10

def to_a = @a

#to_sObject



11
# File 'lib/base_convert/chars.rb', line 11

def to_s = @a.join

#top(x) ⇒ Object



48
# File 'lib/base_convert/chars.rb', line 48

def top(x)    = chars_in(x){@a.delete _1;@a.push _1}