Class: Cabriolet::System::FileHandle
- Inherits:
-
Object
- Object
- Cabriolet::System::FileHandle
- Defined in:
- lib/cabriolet/system/file_handle.rb
Overview
FileHandle provides file I/O operations using the Ruby File class
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
-
#close ⇒ void
Close the file.
-
#closed? ⇒ Boolean
Check if the file is closed.
-
#flush ⇒ void
Flush the file buffer.
-
#initialize(filename, mode) ⇒ FileHandle
constructor
Initialize a new file handle.
-
#read(bytes) ⇒ String
Read bytes from the file.
-
#seek(offset, whence) ⇒ Integer
Seek to a position in the file.
-
#size ⇒ Integer
Get the size of the file.
-
#tell ⇒ Integer
Get current position in the file.
-
#write(data) ⇒ Integer
Write bytes to the file.
Constructor Details
#initialize(filename, mode) ⇒ FileHandle
Initialize a new file handle
14 15 16 17 18 |
# File 'lib/cabriolet/system/file_handle.rb', line 14 def initialize(filename, mode) @filename = filename @mode = mode @file = open_file(mode) end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
7 8 9 |
# File 'lib/cabriolet/system/file_handle.rb', line 7 def filename @filename end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
7 8 9 |
# File 'lib/cabriolet/system/file_handle.rb', line 7 def mode @mode end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the file
77 78 79 80 |
# File 'lib/cabriolet/system/file_handle.rb', line 77 def close @file.flush unless @file.closed? @file.close unless @file.closed? end |
#closed? ⇒ Boolean
Check if the file is closed
85 86 87 |
# File 'lib/cabriolet/system/file_handle.rb', line 85 def closed? @file.closed? end |
#flush ⇒ void
This method returns an undefined value.
Flush the file buffer
70 71 72 |
# File 'lib/cabriolet/system/file_handle.rb', line 70 def flush @file.flush unless @file.closed? end |
#read(bytes) ⇒ String
Read bytes from the file
24 25 26 |
# File 'lib/cabriolet/system/file_handle.rb', line 24 def read(bytes) @file.read(bytes) || "" end |
#seek(offset, whence) ⇒ Integer
Seek to a position in the file
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cabriolet/system/file_handle.rb', line 41 def seek(offset, whence) io_whence = case whence when Constants::SEEK_START then ::IO::SEEK_SET when Constants::SEEK_CUR then ::IO::SEEK_CUR when Constants::SEEK_END then ::IO::SEEK_END else raise ArgumentError, "Invalid whence value: #{whence}" end @file.seek(offset, io_whence) @file.pos end |
#size ⇒ Integer
Get the size of the file
63 64 65 |
# File 'lib/cabriolet/system/file_handle.rb', line 63 def size @file.size end |
#tell ⇒ Integer
Get current position in the file
56 57 58 |
# File 'lib/cabriolet/system/file_handle.rb', line 56 def tell @file.pos end |
#write(data) ⇒ Integer
Write bytes to the file
32 33 34 |
# File 'lib/cabriolet/system/file_handle.rb', line 32 def write(data) @file.write(data) end |