Module: Flydata::Util::FileUtil
- Included in:
- Helper::Action::ActionPosition, Helper::Action::SendLogs
- Defined in:
- lib/flydata/util/file_util.rb
Instance Method Summary collapse
- #read_line(file_path, default_value = nil) ⇒ Object
- #tail(path, num_of_lines) ⇒ Object
- #write_line(file_path, new_value) ⇒ Object
Instance Method Details
#read_line(file_path, default_value = nil) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/flydata/util/file_util.rb', line 11 def read_line(file_path, default_value = nil) ret = nil if FileTest.exist?(file_path) ret = File.readlines(file_path).first.to_s.strip end ret end |
#tail(path, num_of_lines) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/flydata/util/file_util.rb', line 20 def tail(path, num_of_lines) file = File.open(path, "r") buffer_s = 512 line_count = 0 file.seek(0, IO::SEEK_END) offset = file.pos # we start at the end while line_count <= num_of_lines && offset > 0 to_read = if (offset - buffer_s) < 0 offset else buffer_s end file.seek(offset-to_read) data = file.read(to_read) data.reverse.each_char do |c| if line_count > num_of_lines offset += 1 break end offset -= 1 if c == "\n" line_count += 1 end end end file.seek(offset) data = file.read end |
#write_line(file_path, new_value) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/flydata/util/file_util.rb', line 4 def write_line(file_path, new_value) File.delete(file_path) if FileTest.exist?(file_path) File.open(file_path, 'w') do |out| out.write new_value end end |