Class: File

Inherits:
Object show all
Defined in:
lib/rubyexts/file.rb

Class Method Summary collapse

Class Method Details

.add(file, *chunks) ⇒ String

Append given chunks to the existing file or create new one. It use IO#print for write to given file.

Examples:

File.append "~/.bashrc", source # => "/Users/botanicus/.bashrc"

Parameters:

  • file (String)

    Path to file. You can use ~, ~user or similar expressions.

  • *chunks (String, Object)

    Chunks which will be written to the file

Returns:

  • (String)

    Expanded path to given file

Raises:

  • (Errno::ENOENT)

    If parent directory of given file doesn’t exist

  • (Errno::EACCES)

    If you don’t have permissions to write to given file

Author:

  • Botanicus

Since:

  • 0.0.3



32
33
34
# File 'lib/rubyexts/file.rb', line 32

def self.add(file, *chunks)
  self.write(:print, "a", file, *chunks)
end

.append(file, *chunks) ⇒ String

Append given chunks to the existing file or create new one. It use IO#puts for write to given file.

Examples:

File.append "~/.bashrc", source # => "/Users/botanicus/.bashrc"

Parameters:

  • file (String)

    Path to file. You can use ~, ~user or similar expressions.

  • *chunks (String, Object)

    Chunks which will be written to the file

Returns:

  • (String)

    Expanded path to given file

Raises:

  • (Errno::ENOENT)

    If parent directory of given file doesn’t exist

  • (Errno::EACCES)

    If you don’t have permissions to write to given file

Author:

  • Botanicus

Since:

  • 0.0.3



16
17
18
# File 'lib/rubyexts/file.rb', line 16

def self.append(file, *chunks)
  self.write(:puts, "a", file, *chunks)
end

Create new file and write there given chunks. It use IO#print for write to given file.

Examples:

File.print "~/.bashrc", source # => "/Users/botanicus/.bashrc"

Parameters:

  • file (String)

    Path to file. You can use ~, ~user or similar expressions.

  • *chunks (String, Object)

    Chunks which will be written to the file

Returns:

  • (String)

    Expanded path to given file

Raises:

  • (Errno::ENOENT)

    If parent directory of given file doesn’t exist

  • (Errno::EACCES)

    If you don’t have permissions to write to given file

Author:

  • Botanicus

Since:

  • 0.0.3



64
65
66
# File 'lib/rubyexts/file.rb', line 64

def self.print(file, *chunks)
  self.write(:print, "w", file, *chunks)
end

.puts(file, *chunks) ⇒ String

Create new file and write there given chunks. It use IO#puts for write to given file.

Examples:

File.puts "~/.bashrc", source # => "/Users/botanicus/.bashrc"

Parameters:

  • file (String)

    Path to file. You can use ~, ~user or similar expressions.

  • *chunks (String, Object)

    Chunks which will be written to the file

Returns:

  • (String)

    Expanded path to given file

Raises:

  • (Errno::ENOENT)

    If parent directory of given file doesn’t exist

  • (Errno::EACCES)

    If you don’t have permissions to write to given file

Author:

  • Botanicus

Since:

  • 0.0.3



48
49
50
# File 'lib/rubyexts/file.rb', line 48

def self.puts(file, *chunks)
  self.write(:puts, "w", file, *chunks)
end

.write(method, mode, file, *args) ⇒ String

Write data to file with given method.

Examples:

File.write :printf, "w", "~/.bashrc", "%d %04x", 123, 123 # => "/Users/botanicus/.bashrc"

Parameters:

  • file (String)

    Path to file. You can use ~, ~user or similar expressions.

  • *chunks (String, Object)

    Chunks which will be written to the file

Returns:

  • (String)

    Expanded path to given file

Raises:

  • (Errno::ENOENT)

    If parent directory of given file doesn’t exist

  • (Errno::EACCES)

    If you don’t have permissions to write to given file

Author:

  • Botanicus

Since:

  • 0.0.3



79
80
81
82
83
# File 'lib/rubyexts/file.rb', line 79

def self.write(method, mode, file, *args)
  self.expand_path(file).tap do |path|
    self.open(path, mode) { |file| file.send(method, *args) }
  end
end