Difference between revisions of "Text File Writer Assignment"
Jump to navigation
Jump to search
(→Ensure) |
(→Ensure) |
||
Line 3: | Line 3: | ||
Ruby provides begin/rescue/ensure which is rather analogous to try/catch/finally in Java. | 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 | + | note: although coincidental, it is rather fitting that this reference covers [https://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/ensure.html 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== | ||
+ | [https://www.rubyguides.com/2019/12/yield-keyword/ yield] | ||
=Code To Implement= | =Code To Implement= |
Revision as of 05:06, 25 November 2022
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
Code To Implement
open_yield_close
file: | src/main/ruby/text_file_writer/text_file_writer.rb | |
class: | TextFileWriter | |
superclass: | Object | |
methods: | open_yield_close(path) |
Client
TextFileWriter.open_yield_close(path) do |file| file.write("<html><body><h1>#{Time.now}</h1></body></html>") end