Class: File
- Inherits:
-
Object
- Object
- File
- Defined in:
- lib/knut_tools/file__END__.rb
Overview
File-extensions for Ruby-scripts.
Inside Ruby-scripts text after __END__ is used like a “internal files”.
Sometimes you want to use this data from other scripts.
This extensions of the File-class allows to open Files, starting at __END__.
Details see
-
File.open_after_END
-
File.readlines_after_END
-
File.each_line_after_END
Class Method Summary collapse
-
.each_line_after_END(filename) ⇒ Object
Get all lines after __END__.
-
.open_after_END(filename) ⇒ Object
This methods behave like File#open, but it starts after __END__.
-
.readlines_after_END(filename) ⇒ Object
Return content of script after __END__.
Class Method Details
.each_line_after_END(filename) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/knut_tools/file__END__.rb', line 75 def each_line_after_END( filename ) f = open_after_END( filename ) res = true if block_given? res = f.each_line{|line| yield line } else res = f.readlines end f.close return res end |
.open_after_END(filename) ⇒ Object
This methods behave like File#open, but it starts after __END__
Example:
filehandle = File.open_after_END('testdata/hello_world.rb')
filehandle.eachline{|line|
... #text after __END__
}
filehandle.close
or better:
File.open_after_END('testdata/hello_world.rb'){|filehandle|
filehandle.eachline{|line|
... #text after __END__
}
}
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/knut_tools/file__END__.rb', line 35 def open_after_END( filename ) f = File.new(filename, 'r') if filename =~ /\.rbw?\Z/ line = nil while line !~ /^__END__\n/ and ! f.eof? line = f.readline end end if block_given? #~ f.each_line{|line| yield line } yield f else return f end #block_given? end |
.readlines_after_END(filename) ⇒ Object
Return content of script after __END__
Example:
File.readlines_after_END('testdata/hello_world.rb').each{|line|
#...
}
59 60 61 62 63 64 |
# File 'lib/knut_tools/file__END__.rb', line 59 def readlines_after_END( filename ) f = open_after_END( filename ) txt = f.readlines.join f.close return txt end |