Class: NameFinder::Buffer

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

Instance Method Summary collapse

Constructor Details

#initialize(string, position = 0) ⇒ Buffer

Returns a new instance of Buffer.



3
4
5
6
7
# File 'lib/name_finder/buffer.rb', line 3

def initialize(string, position = 0)
  @string = string
  @position = position
  @length = string.length
end

Instance Method Details

#advance_by(n) ⇒ Object



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

def advance_by(n)
  new(@position + n)
end

#advance_past(delimiter) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/name_finder/buffer.rb', line 13

def advance_past(delimiter)
  p = (@position ... @length).find { |i| @string[i] == delimiter }
  if p
    new(p + 1)
  else
    new(@length)
  end
end

#at_end?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/name_finder/buffer.rb', line 22

def at_end?
  @position >= @length
end

#headObject



26
27
28
# File 'lib/name_finder/buffer.rb', line 26

def head
  @string[@position, 1]
end

#inspect(*args) ⇒ Object



34
35
36
# File 'lib/name_finder/buffer.rb', line 34

def inspect(*args)
  "<Buffer:#{@string[@position .. -1].inspect}>"
end

#restObject



30
31
32
# File 'lib/name_finder/buffer.rb', line 30

def rest
  new(@position + 1)
end