[LRUG] Understanding Ruby Tempfile documentation

Jonathon Horsman jonathon at arctickiwi.com
Tue Jan 26 04:39:47 PST 2016


Makes sense, thanks.

So what's the advantage of this instead of directly subclassing File?

Using this example, couldn't you equally...

class Tempfile < File
    def initialize basename, temp_dir = '/tmp'
      file_name = [ basename, rand(1_000_000_000), Time.now.to_f ].join('-')
      file_path = File.join temp_dir, file_name
      *open file_path, ... # calls up to File*
    end

Thanks again

On 26 January 2016 at 12:32, Craig R Webster <craig at xeriom.net> wrote:

> Hi Jonathon,
>
> Looks like it’s Ruby all the way down:
>
>   http://ruby-doc.org/stdlib-2.2.2/libdoc/delegate/rdoc/Object.html
>
> 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):
>
>   class Tempfile
>     def initialize basename, temp_dir = '/tmp'
>       file_name = [ basename, rand(1_000_000_000), Time.now.to_f
> ].join('-')
>       file_path = File.join temp_dir, file_name
>       @file = File.open file_path, ...
>     end
>
>     def method_missing name, *args
>       @file.send name, *args
>     end
>
>     def respond_to? name
>       @file.respond_to? name
>     end
>   end
>
> The resulting class will support the same API and behaviour as the class
> delegated to, except where the behaviour is overwritten.
>
> Cheers,
> Craig
>
>
> On 26 Jan 2016, at 12:05, Jonathon Horsman <jonathon at arctickiwi.com>
> wrote:
>
> Hi
>
> Trying to understand Tempfile, so reading the docs:
>
> http://docs.ruby-lang.org/en/2.2.0/Tempfile.html
>
> I assume it's a subclass of IO, but I can't see any mention of this.
>
> It does however say:
>
> *Parent:*
> *DelegateClass(File)*
>
> what does this mean?
>
> Thanks in advance
>
> Jonno
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
>
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20160126/bc5340fe/attachment-0002.html>


More information about the Chat mailing list