Class: LineInput
Overview
$Id: lineinput.rb 2226 2006-04-15 03:05:09Z aamine $
Copyright © 2002-2005 Minero Aoki
This program is free software. You can distribute/modify this program under the terms of the GNU LGPL, Lesser General Public License version 2.1.
Direct Known Subclasses
Instance Method Summary collapse
- #each ⇒ Object
- #eof? ⇒ Boolean
- #getlines_until(re) ⇒ Object (also: #break)
- #getlines_while(re) ⇒ Object (also: #span)
- #gets ⇒ Object
-
#initialize(f) ⇒ LineInput
constructor
A new instance of LineInput.
- #inspect ⇒ Object
- #lineno ⇒ Object
- #ungets(line) ⇒ Object
- #until_match(re) ⇒ Object
- #until_terminator(re) ⇒ Object
- #while_match(re) ⇒ Object
Constructor Details
#initialize(f) ⇒ LineInput
Returns a new instance of LineInput.
13 14 15 16 17 18 |
# File 'lib/lineinput.rb', line 13 def initialize(f) @input = f @buf = [] @lineno = 0 @eof_p = false end |
Instance Method Details
#each ⇒ Object
51 52 53 54 55 |
# File 'lib/lineinput.rb', line 51 def each while line = gets() yield line end end |
#eof? ⇒ Boolean
24 25 26 |
# File 'lib/lineinput.rb', line 24 def eof? @eof_p end |
#getlines_until(re) ⇒ Object Also known as: break
89 90 91 92 93 94 95 |
# File 'lib/lineinput.rb', line 89 def getlines_until(re) buf = [] until_match(re) do |line| buf.push line end buf end |
#getlines_while(re) ⇒ Object Also known as: span
68 69 70 71 72 73 74 |
# File 'lib/lineinput.rb', line 68 def getlines_while(re) buf = [] while_match(re) do |line| buf.push line end buf end |
#gets ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lineinput.rb', line 32 def gets unless @buf.empty? @lineno += 1 return @buf.pop end return nil if @eof_p # to avoid ARGF blocking. line = @input.gets @eof_p = true unless line @lineno += 1 line end |
#inspect ⇒ Object
20 21 22 |
# File 'lib/lineinput.rb', line 20 def inspect "\#<#{self.class} file=#{@input.inspect} line=#{lineno()}>" end |
#lineno ⇒ Object
28 29 30 |
# File 'lib/lineinput.rb', line 28 def lineno @lineno end |
#ungets(line) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/lineinput.rb', line 44 def ungets(line) return unless line @lineno -= 1 @buf.push line line end |
#until_match(re) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/lineinput.rb', line 78 def until_match(re) while line = gets() if re =~ line ungets line return end yield line end nil end |
#until_terminator(re) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/lineinput.rb', line 99 def until_terminator(re) while line = gets() return if re =~ line # discard terminal line yield line end nil end |
#while_match(re) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/lineinput.rb', line 57 def while_match(re) while line = gets() unless re =~ line ungets line return end yield line end nil end |