Class: Seto::Editor
- Inherits:
-
Object
- Object
- Seto::Editor
- Defined in:
- lib/seto/editor.rb
Instance Attribute Summary collapse
-
#current_line ⇒ Object
readonly
Returns the value of attribute current_line.
-
#line_number ⇒ Object
readonly
Returns the value of attribute line_number.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
- #append(text) ⇒ Object
- #change(text) ⇒ Object
- #copy ⇒ Object
- #cover?(first, last) ⇒ Boolean
- #delete ⇒ Object
- #exchange ⇒ Object
- #get ⇒ Object
- #hold ⇒ Object
-
#initialize(enumerator) ⇒ Editor
constructor
A new instance of Editor.
- #insert(text) ⇒ Object
- #lineno ⇒ Object
- #load ⇒ Object
- #match?(condition) ⇒ Boolean
- #next ⇒ Object
- #print ⇒ Object
- #quit ⇒ Object
- #read(filename) ⇒ Object
- #substitute(pattern, replace, flag = nil) ⇒ Object
- #transform(pattern, replace) ⇒ Object
- #write(filename) ⇒ Object
Constructor Details
#initialize(enumerator) ⇒ Editor
Returns a new instance of Editor.
4 5 6 7 8 9 |
# File 'lib/seto/editor.rb', line 4 def initialize(enumerator) @enumerator = enumerator @patterns = {} @result = [] @hold_space = [] end |
Instance Attribute Details
#current_line ⇒ Object (readonly)
Returns the value of attribute current_line.
3 4 5 |
# File 'lib/seto/editor.rb', line 3 def current_line @current_line end |
#line_number ⇒ Object (readonly)
Returns the value of attribute line_number.
3 4 5 |
# File 'lib/seto/editor.rb', line 3 def line_number @line_number end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
3 4 5 |
# File 'lib/seto/editor.rb', line 3 def result @result end |
Instance Method Details
#append(text) ⇒ Object
15 16 17 |
# File 'lib/seto/editor.rb', line 15 def append(text) @current_line += text end |
#change(text) ⇒ Object
19 20 21 |
# File 'lib/seto/editor.rb', line 19 def change(text) @current_line = text end |
#copy ⇒ Object
41 42 43 |
# File 'lib/seto/editor.rb', line 41 def copy @result << @current_line.dup end |
#cover?(first, last) ⇒ Boolean
100 101 102 103 104 105 |
# File 'lib/seto/editor.rb', line 100 def cover?(first, last) case [first.class, last.class] when [Fixnum, Fixnum] then cover1 first, last else cover2 first, last end end |
#delete ⇒ Object
23 24 25 |
# File 'lib/seto/editor.rb', line 23 def delete @current_line = '' end |
#exchange ⇒ Object
53 54 55 56 |
# File 'lib/seto/editor.rb', line 53 def exchange @current_line, hs = @hold_space.pop, @current_line @hold_space << hs end |
#get ⇒ Object
45 46 47 |
# File 'lib/seto/editor.rb', line 45 def get @current_line = @hold_space.pop end |
#hold ⇒ Object
49 50 51 |
# File 'lib/seto/editor.rb', line 49 def hold @hold_space << @current_line.dup end |
#insert(text) ⇒ Object
27 28 29 |
# File 'lib/seto/editor.rb', line 27 def insert(text) @current_line = "#{text}#{@current_line}" end |
#lineno ⇒ Object
58 59 60 |
# File 'lib/seto/editor.rb', line 58 def lineno Kernel.print @line_number end |
#load ⇒ Object
11 12 13 |
# File 'lib/seto/editor.rb', line 11 def load @current_line, @line_number = @enumerator.next end |
#match?(condition) ⇒ Boolean
91 92 93 94 95 96 97 98 |
# File 'lib/seto/editor.rb', line 91 def match?(condition) case condition when Fixnum then condition == @line_number when Regexp then condition =~ @current_line else condition end end |
#next ⇒ Object
62 63 64 65 |
# File 'lib/seto/editor.rb', line 62 def next copy load end |
#print ⇒ Object
67 68 69 |
# File 'lib/seto/editor.rb', line 67 def print Kernel.print @current_line end |
#quit ⇒ Object
71 72 73 74 |
# File 'lib/seto/editor.rb', line 71 def quit copy raise StopIteration end |
#read(filename) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/seto/editor.rb', line 76 def read(filename) begin text = open(filename).read rescue # ignore end append text if text end |
#substitute(pattern, replace, flag = nil) ⇒ Object
31 32 33 34 35 |
# File 'lib/seto/editor.rb', line 31 def substitute(pattern, replace, flag=nil) method = :sub! method = :gsub! if flag && flag == :g @current_line.send method, pattern, replace end |
#transform(pattern, replace) ⇒ Object
37 38 39 |
# File 'lib/seto/editor.rb', line 37 def transform(pattern, replace) @current_line.tr! pattern, replace end |
#write(filename) ⇒ Object
85 86 87 88 89 |
# File 'lib/seto/editor.rb', line 85 def write(filename) open(filename, 'a') do |f| f.write @current_line end end |