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

<<(contents)
Alias for: append
append(contents) click to toggle source

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
Also aliased as: <<
cat()
Alias for: contents
contents() click to toggle source

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
Also aliased as: read, cat
contents_or_blank() click to toggle source

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() click to toggle source

Create a blank file.

# File lib/rush/file.rb, line 13
def create
  write('')
  self
end
Also aliased as: touch
dir?() click to toggle source
# File lib/rush/file.rb, line 4
def dir?
  false
end
dirname() click to toggle source
# File lib/rush/file.rb, line 8
def dirname
  ::File.dirname full_path
end
entries() click to toggle source
# File lib/rush/file.rb, line 97
def entries
  [ self ]
end
gsub(pattern, replace_with)
Alias for: replace_contents!
line_count() click to toggle source

Count the number of lines in the file.

# File lib/rush/file.rb, line 84
def line_count
  lines.size
end
lines() click to toggle source

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
lines_or_empty() click to toggle source

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
read()
Alias for: contents
replace_contents!(pattern, replace_with) click to toggle source

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
Also aliased as: gsub, s
s(pattern, replace_with)

Because I like vim.

Alias for: replace_contents!
size() click to toggle source

Size in bytes on disk.

# File lib/rush/file.rb, line 26
def size
  stat[:size]
end
touch()
Alias for: create
write(new_contents) click to toggle source

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