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

alexadominguez--practice2 #38

Open
wants to merge 5 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
Binary file added .DS_Store
Binary file not shown.
40 changes: 40 additions & 0 deletions archiver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Archiver
attr_accessor :file
@@modes = %i[reader writer both]

@@modes.each do |mode|
define_method("open_file_as_#{mode}") do |file|
@file = File.open(file, "r") if mode == :reader
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer use "" for interpolations. If you use just string ''

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, I will change this.

@file = File.open(file, "w") if mode == :writer
@file = File.open(file, "r+") if mode == :both
end
end

def read_file lines: false
lines ? file.read : file.readlines.map(&:chomp)
end

def write_file data: nil, append: true
append ? File.write(file, data, mode: "a") : File.write(file, data)
end
end

=begin
# Reading a file using new code
test = Archiver.new
test.open_file_as_both("testing_text.txt")
test.write_file(data: "Still testing", append: true)
p test.read_file
test.file.close
=end

=begin
# Reading a file using original code given
file = Tempfile.new('foo')
file.path # => A unique filename in the OS's temp directory,
file.write("hello world")
file.rewind
file.read # => "hello world"
file.close
file.unlink # deletes the temp file
=end
3 changes: 3 additions & 0 deletions testing_text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hi I´m Alexa but not from Amazon.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lscantillowl hahaha now it is my personal jk

Test1
Test2