Class: TxtFileHandler
- Inherits:
-
Object
- Object
- TxtFileHandler
- Defined in:
- lib/txt_file_handler.rb
Overview
Lil lib used to read txt files
Class Method Summary collapse
Instance Method Summary collapse
- #clear! ⇒ Object
- #file ⇒ Object
-
#initialize(fileadr, relative = true) ⇒ TxtFileHandler
constructor
A new instance of TxtFileHandler.
- #line(nr) ⇒ Object
- #line_nr ⇒ Object
- #lines(start, finish) ⇒ Object
Constructor Details
#initialize(fileadr, relative = true) ⇒ TxtFileHandler
Returns a new instance of TxtFileHandler.
3 4 5 6 7 8 9 10 |
# File 'lib/txt_file_handler.rb', line 3 def initialize(fileadr, relative = true) if relative @path = File.join(Rails.root, fileadr) else @path = fileadr end end |
Class Method Details
.exist?(fileadr, relative = true) ⇒ Boolean
68 69 70 71 72 73 74 75 |
# File 'lib/txt_file_handler.rb', line 68 def self.exist?(fileadr, relative = true) if relative path = File.join(Rails.root, fileadr) else path = fileadr end FileTest.exist?(path) end |
Instance Method Details
#clear! ⇒ Object
77 78 79 80 81 82 |
# File 'lib/txt_file_handler.rb', line 77 def clear! close return unless wopen @file << "" close end |
#file ⇒ Object
64 65 66 |
# File 'lib/txt_file_handler.rb', line 64 def file @file end |
#line(nr) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/txt_file_handler.rb', line 12 def line(nr) return unless ropen line = nil i = 0 @file.each_line{ |s| if i == nr line = s close break end i+=1 } return line end |
#line_nr ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/txt_file_handler.rb', line 27 def line_nr return unless ropen i = 0 @file.each_line{ |s| i+=1 } close i end |
#lines(start, finish) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/txt_file_handler.rb', line 37 def lines(start, finish) return unless ropen lines = [] if start > finish temp = start start = finish finish = temp invert = true end i = 0 j = 0 @file.each_line{ |s| if (i >= start && i <= finish) lines[j] = s.gsub("", "Ruby") j+=1 end i+=1 } if invert lines = lines.reverse end close lines end |