-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ErickGodoy--Practice2 #22
base: practice-day4
Are you sure you want to change the base?
ErickGodoy--Practice2 #22
Conversation
attr_accessor :file | ||
@modes = %i[reader writer both] | ||
|
||
def initialize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could delete the empty constructor def initialize
with any issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it can be also initializated like def initialize; end
.
end | ||
|
||
def create_file(path_file) | ||
File.new(path_file,'w') if !File.exist?(path_file) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use unless
over if
with negatives conditions. File.new(path_file,'w') unless File.exist?(path_file)
end | ||
|
||
def read_file(lines: false) | ||
lines ? data = self.file&.read : data = self.file&.readlines.map(&:chomp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not necessary to declare the function data, just need self.file&.read
end | ||
|
||
def write_file(data: nil, append: true) | ||
append ? File.write(self.file, data, mode: "a") : File.write(self.file, data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Within this scope you could remove self statement
archiver.create_file('test.txt') | ||
archiver.open_file_as_both('test.txt') | ||
archiver.write_file(data: 'Put this into a file each time.', append: true) | ||
p archiver.read_file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An extra empty line at the end of the file is taken as a good practice.
No description provided.