Class: Raabro::Input

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, offset = 0, options = {}) ⇒ Input

Returns a new instance of Input.



12
13
14
15
16
17
# File 'lib/raabro.rb', line 12

def initialize(string, offset=0, options={})

  @string = string
  @offset = offset.is_a?(Hash) ? 0 : offset
  @options = offset.is_a?(Hash) ? offset : options
end

Instance Attribute Details

#offsetObject

Returns the value of attribute offset.



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

def offset
  @offset
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/raabro.rb', line 10

def options
  @options
end

#stringObject

Returns the value of attribute string.



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

def string
  @string
end

Instance Method Details

#at(i) ⇒ Object



39
40
41
42
# File 'lib/raabro.rb', line 39

def at(i)

  @string[i, 1]
end

#match(str_rex_or_block) ⇒ Object



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

def match(str_rex_or_block)

  if str_rex_or_block.is_a?(Regexp)
    m = @string[@offset..-1].match(str_rex_or_block)
    m && (m.offset(0).first == 0) ? m[0].length : false
  elsif str_rex_or_block.is_a?(Proc)
    str_rex_or_block.call(
      *[ @string[@offset..-1], self ].take(str_rex_or_block.arity))
  else # String or whatever responds to #to_s
    s = str_rex_or_block.to_s
    l = s.length
    @string[@offset, l] == s ? l : false
  end
end

#tring(l = -1)) ⇒ Object



34
35
36
37
# File 'lib/raabro.rb', line 34

def tring(l=-1)

  l < 0 ? @string[@offset..l] : @string[@offset, l]
end