Class: FileIO

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path = "../data/game_log.json") ⇒ FileIO

Returns a new instance of FileIO


6
7
8
9
10
11
12
13
# File 'lib/FileIO.rb', line 6

def initialize(file_path = "../data/game_log.json")
  @path = file_path
  unless File.file?(file_path)
    @file = File.new(file_path, "a+")
  else
    @file = File.open(file_path, "a+")
  end
end

Instance Attribute Details

#fileObject

Returns the value of attribute file


4
5
6
# File 'lib/FileIO.rb', line 4

def file
  @file
end

#pathObject

Returns the value of attribute path


4
5
6
# File 'lib/FileIO.rb', line 4

def path
  @path
end

Instance Method Details

#read(path = nil) ⇒ Object


27
28
29
30
31
32
# File 'lib/FileIO.rb', line 27

def read(path = nil)
  path ||= @path
  file = File.read(path)
  data = JSON.parse(file)
  data
end

#write(data, path = nil) ⇒ Object


15
16
17
18
19
20
# File 'lib/FileIO.rb', line 15

def write(data, path = nil)
  path ||= @path
  File.open(path, "w") do |f|
    f.puts data.to_json
  end
end

#write_line(text) ⇒ Object


23
24
25
# File 'lib/FileIO.rb', line 23

def write_line(text)

end