-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'tempfile' | ||
require 'fileutils' | ||
require 'find' | ||
|
||
class Archiver | ||
attr_accessor :file | ||
@modes = %i[reader writer both] | ||
|
||
def initialize | ||
end | ||
|
||
@modes.each do |mode| | ||
define_method("open_file_as_#{mode}") do |file| | ||
@file = File.open(file, 'r') if mode == :reader | ||
@file = File.open(file, 'w') if mode == :writer | ||
@file = File.open(file, 'r+') if mode == :both | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. It is not necessary to declare the function data, just need |
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Within this scope you could remove self statement |
||
end | ||
end | ||
|
||
#TEST | ||
archiver = Archiver.new | ||
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 commentThe 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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Put this into a file each time.Put this into a file each time. |
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 issueThere 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
.