Difference between revisions of "Text File Writer Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
 
(8 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
==Yield==
 
==Yield==
 
[https://www.rubyguides.com/2019/12/yield-keyword/ yield]
 
[https://www.rubyguides.com/2019/12/yield-keyword/ yield]
 +
 +
[https://apidock.com/ruby/Kernel/block_given%3F block_given?]
 +
 +
==File==
 +
[https://apidock.com/ruby/v2_5_5/File/new/class file = File.new(path, mode)]
 +
 +
[https://ruby-doc.org/core-2.7.0/IO.html#method-i-close file.close()]
  
 
=Code To Implement=
 
=Code To Implement=
==TextFileUtils==
+
==TextFileYieldAndRescue==
{{RubyToImplement|text_file_utils|text_file_utils|TextFileUtils|Object|write_text(path)}}
+
{{RubyToImplement|text_file_yield_and_rescue|text_file_yield_and_rescue|TextFileYieldAndRescue|Object|write_text(path)}}
 
==write_text(path)==
 
==write_text(path)==
 +
Create a new File for the specified path in write mode.
 +
 +
If a block is given by the client, yield the file to that block (which will presumably write to the file), and ensure that you close the file.  Return the closed file.
 +
 +
If no block is given, return the opened file.
  
 
=Client=
 
=Client=
<nowiki> TextFileWriter.open_yield_close(path) do |file|
+
{{RubyToRun|text_file_yield_and_rescue|text_file_yield_and_rescue|}}
 +
 
 +
In the same file you edit class TextFileYieldAndRescue, you will find a simple client:
 +
 
 +
<syntaxhighlight lang="ruby">
 +
  TextFileYieldAndRescue.write_text(path) do |file|
 
     file.write("<html><body><h1>#{Time.now}</h1></body></html>")
 
     file.write("<html><body><h1>#{Time.now}</h1></body></html>")
   end</nowiki>
+
   end
 +
</syntaxhighlight>
 +
 
 +
=Testing Your Solution=
 +
{{RubyUnitTest|text_file_yield_and_rescue|text_file_yield_and_rescue}}

Latest revision as of 20:36, 4 May 2023

Background

Ensure

Ruby provides begin/rescue/ensure which is rather analogous to try/catch/finally in Java.

note: although coincidental, it is rather fitting that this reference covers begin/rescue/ensure with ensuring to close an opened file as its example. Ensuring to close an opened file is the canonical example for this sort of feature.

Yield

yield

block_given?

File

file = File.new(path, mode)

file.close()

Code To Implement

TextFileYieldAndRescue

file: src/main/ruby/text_file_yield_and_rescue/text_file_yield_and_rescue.rb Ruby logo.svg
class: TextFileYieldAndRescue
superclass: Object
methods: write_text(path)

write_text(path)

Create a new File for the specified path in write mode.

If a block is given by the client, yield the file to that block (which will presumably write to the file), and ensure that you close the file. Return the closed file.

If no block is given, return the opened file.

Client

file to run: src//ruby/text_file_yield_and_rescue/text_file_yield_and_rescue.rb Ruby logo.svg

In the same file you edit class TextFileYieldAndRescue, you will find a simple client:

  TextFileYieldAndRescue.write_text(path) do |file|
    file.write("<html><body><h1>#{Time.now}</h1></body></html>")
  end

Testing Your Solution

file: src/test/ruby/text_file_yield_and_rescue/text_file_yield_and_rescue.rb Ruby logo.svg UnitTest

note: ensure that you have removed all printing to receive credit for any assignment.