Class: ByteBoozer2::File

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

Overview

This class implements file handling related helper methods.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/byteboozer2/file.rb', line 6

def data
  @data
end

#nameObject

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

#readObject



21
22
23
# File 'lib/byteboozer2/file.rb', line 21

def read
  @data = IO.binread(@name).unpack('C*')
end

#writeObject



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