class Rush::File
Files are a subclass of Rush::Entry
. Most of the file-specific operations relate to manipulating the file's contents, like search and replace.
Public Instance Methods
Append new contents to the end of the file, keeping what was in it.
# File lib/rush/file.rb, line 47 def append(contents) connection.append_to_file(full_path, contents) end
Raw contents of the file. For non-text files, you probably want to avoid printing this on the screen.
# File lib/rush/file.rb, line 32 def contents connection.file_contents(full_path) end
Return the file's contents, or if it doesn't exist, a blank string.
# File lib/rush/file.rb, line 77 def contents_or_blank contents rescue Rush::DoesNotExist "" end
Create a blank file.
# File lib/rush/file.rb, line 13 def create write('') self end
# File lib/rush/file.rb, line 4 def dir? false end
# File lib/rush/file.rb, line 8 def dirname ::File.dirname full_path end
# File lib/rush/file.rb, line 97 def entries [ self ] end
Count the number of lines in the file.
# File lib/rush/file.rb, line 84 def line_count lines.size end
Return an array of lines from the file, similar to stdlib's File#readlines.
# File lib/rush/file.rb, line 53 def lines contents.split("\n") end
Return an array of lines, or an empty array if the file does not exist.
# File lib/rush/file.rb, line 89 def lines_or_empty lines rescue Rush::DoesNotExist [] end
Hardlink the file (see File.ln for options)
# File lib/rush/file.rb, line 20 def link(dst, options = {}) connection.ln(full_path, dst, options) self.class.new(dst, box) end
Search-and-replace file contents.
Example: box.replace_contents!(/localhost/, 'local.host')
# File lib/rush/file.rb, line 69 def replace_contents!(pattern, replace_with) write contents.gsub(pattern, replace_with) end
Search the file's for a regular expression. Returns nil if no match, or each of the matching lines in its entirety.
Example: box.search(/localhost/) # -> [ “127.0.0.1 localhostn”, “::1 localhostn” ]
# File lib/rush/file.rb, line 61 def search(pattern) matching_lines = lines.select { |line| line.match(pattern) } matching_lines.size == 0 ? nil : matching_lines end
Size in bytes on disk.
# File lib/rush/file.rb, line 26 def size stat[:size] end
Write to the file, overwriting whatever was already in it.
Example: file.write “hello, worldn”
# File lib/rush/file.rb, line 42 def write(new_contents) connection.write_file(full_path, new_contents) end