Class: BaseConvert::Chars

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start = 32, stop = 126) ⇒ Chars

Returns a new instance of Chars.



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

def initialize(start=32, stop=126)
  @start,@stop = start,stop
end

Instance Attribute Details

#startObject

Returns the value of attribute start.



3
4
5
# File 'lib/base_convert/chars.rb', line 3

def start
  @start
end

#stopObject

Returns the value of attribute stop.



3
4
5
# File 'lib/base_convert/chars.rb', line 3

def stop
  @stop
end

Instance Method Details

#add(x) ⇒ Object



42
43
44
45
46
# File 'lib/base_convert/chars.rb', line 42

def add(x)
  chars_in(x) do |c|
    self.push(c) unless self.include?(c)
  end
end

#chars_in(x) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/base_convert/chars.rb', line 24

def chars_in(x)
  case x
  when Regexp
    @start.upto(@stop).each do |l|
      c = l.chr(Encoding::UTF_8)
      yield c if x.match? c
    end
  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



55
56
57
# File 'lib/base_convert/chars.rb', line 55

def remove(x)
  chars_in(x){|c| self.delete(c)}
end

#set(s) ⇒ Object

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)



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/base_convert/chars.rb', line 12

def set(s)
  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)
  else
    raise 'expected /^([ij]\d+)|([vw]\h+)$/'
  end
end

#top(x) ⇒ Object



48
49
50
51
52
53
# File 'lib/base_convert/chars.rb', line 48

def top(x)
  chars_in(x) do |c|
    self.delete(c)
    self.push(c)
  end
end