Skip to content
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

Open
wants to merge 2 commits into
base: practice-day4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions archiver.rb
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
Copy link
Collaborator

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

Copy link
Collaborator

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

@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)
Copy link
Collaborator

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)
Copy link
Collaborator

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)
Copy link
Collaborator

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

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
Copy link
Collaborator

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.

1 change: 1 addition & 0 deletions test.txt
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.