Class: ByteBoozer2::File
- Inherits:
-
Object
- Object
- ByteBoozer2::File
- Defined in:
- lib/byteboozer2/file.rb
Overview
This class implements file handling related helper methods.
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(name, data = nil) ⇒ File
constructor
A new instance of File.
- #read ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(name, data = nil) ⇒ File
Returns a new instance of File.
16 17 18 19 |
# File 'lib/byteboozer2/file.rb', line 16 def initialize(name, data = nil) @name = name @data = data end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
6 7 8 |
# File 'lib/byteboozer2/file.rb', line 6 def data @data end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/byteboozer2/file.rb', line 6 def name @name end |
Class Method Details
.load(*args) ⇒ Object
8 9 10 |
# File 'lib/byteboozer2/file.rb', line 8 def self.load(*args) new(*args).tap(&:read) end |
.save(*args) ⇒ Object
12 13 14 |
# File 'lib/byteboozer2/file.rb', line 12 def self.save(*args) new(*args).tap(&:write) end |
Instance Method Details
#read ⇒ Object
21 22 23 |
# File 'lib/byteboozer2/file.rb', line 21 def read @data = IO.binread(@name).unpack('C*') end |
#write ⇒ Object
25 26 27 28 29 |
# File 'lib/byteboozer2/file.rb', line 25 def write ::File.open(@name, ::File::WRONLY | ::File::CREAT | ::File::EXCL, binmode: true, encoding: 'ASCII-8BIT') do |file| file.write @data.pack('C*') end end |