<div dir="ltr">From Pickaxe book:<div><br></div><div><div>" Class Tempfile creates managed temporary files. Although they behave the same as any other IO objects, temporary files are automatically deleted when the Ruby program terminates. Once a Tempfile object has been created, the underlying file may be opened and closed a number of times in succession.</div><div><br></div><div>Tempfile does not directly inherit from IO. Instead, it delegates calls to a File object. From the programmer’s perspective, apart from the unusual new, open, and close semantics, a Tempfile object behaves as if it were an IO object.</div><div><br></div><div>If you don’t specify a directory to hold temporary files when you create them, the tmpdir library will be used to find a system-dependent location. "</div></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 26 January 2016 at 12:39, Jonathon Horsman <span dir="ltr"><<a href="mailto:jonathon@arctickiwi.com" target="_blank">jonathon@arctickiwi.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Makes sense, thanks.<div><br></div><div>So what's the advantage of this instead of directly subclassing File?</div><div><br></div><div>Using this example, couldn't you equally...</div><div><br></div><div><div style="font-size:12.8px">class Tempfile < File</div><span class=""><div style="font-size:12.8px">    def initialize basename, temp_dir = '/tmp'</div><div style="font-size:12.8px">      file_name = [ basename, rand(1_000_000_000), Time.now.to_f ].join('-')</div><div style="font-size:12.8px">      file_path = File.join temp_dir, file_name</div></span><div style="font-size:12.8px">      <b>open file_path, ... # calls up to File</b></div><div style="font-size:12.8px">    end</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Thanks again</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On 26 January 2016 at 12:32, Craig R Webster <span dir="ltr"><<a href="mailto:craig@xeriom.net" target="_blank">craig@xeriom.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hi Jonathon,<div><br></div><div>Looks like it’s Ruby all the way down:</div><div><br></div><div>  <a href="http://ruby-doc.org/stdlib-2.2.2/libdoc/delegate/rdoc/Object.html" target="_blank">http://ruby-doc.org/stdlib-2.2.2/libdoc/delegate/rdoc/Object.html</a></div><div><br></div><div>The example is a little hard to read at a glance, but this is a method to allow composition instead of inheritance. One might rewrite the code something like this to make it more obvious what’s being attempted in this case (with apologies for quick hackiness and glossed-over subtleties):</div><div><br></div><div>  class Tempfile</div><div>    def initialize basename, temp_dir = '/tmp'</div><div>      file_name = [ basename, rand(1_000_000_000), Time.now.to_f ].join('-')</div><div>      file_path = File.join temp_dir, file_name</div><div>      @file = File.open file_path, ...</div><div>    end</div><div><br></div><div>    def method_missing name, *args</div><div>      @file.send name, *args</div><div>    end</div><div><br></div><div>    def respond_to? name</div><div>      @file.respond_to? name</div><div>    end</div><div>  end</div><div><br></div><div>The resulting class will support the same API and behaviour as the class delegated to, except where the behaviour is overwritten. </div><div><br></div><div>Cheers,</div><div>Craig</div><div><br></div><div><br></div><div><div><blockquote type="cite"><div><div><div>On 26 Jan 2016, at 12:05, Jonathon Horsman <<a href="mailto:jonathon@arctickiwi.com" target="_blank">jonathon@arctickiwi.com</a>> wrote:</div><br></div></div><div><div><div><div dir="ltr">Hi<div><br></div><div>Trying to understand Tempfile, so reading the docs:</div><div><br></div><div><a href="http://docs.ruby-lang.org/en/2.2.0/Tempfile.html" target="_blank">http://docs.ruby-lang.org/en/2.2.0/Tempfile.html</a><br></div><div><br></div><div>I assume it's a subclass of IO, but I can't see any mention of this.</div><div><br></div><div>It does however say:</div><div><b><br></b></div><div><b>Parent:</b></div><b>DelegateClass(File)</b><div><b><br></b></div><div>what does this mean?</div><div><br></div><div>Thanks in advance</div><div><br></div><div>Jonno</div></div></div></div><span>
_______________________________________________<br>Chat mailing list<br><a href="mailto:Chat@lists.lrug.org" target="_blank">Chat@lists.lrug.org</a><br>Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org" target="_blank">http://lists.lrug.org/pipermail/chat-lrug.org</a><br>Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/options.cgi/chat-lrug.org</a><br>List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br></span></div></blockquote></div><br></div></div><br>_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org" target="_blank">Chat@lists.lrug.org</a><br>
Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/pipermail/chat-lrug.org</a><br>
Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/options.cgi/chat-lrug.org</a><br>
List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
<br></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/pipermail/chat-lrug.org</a><br>
Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/options.cgi/chat-lrug.org</a><br>
List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
<br></blockquote></div><br></div>