[LRUG] Understanding Ruby Tempfile documentation

Craig R Webster craig at xeriom.net
Tue Jan 26 04:32:14 PST 2016


Hi Jonathon,

Looks like it’s Ruby all the way down:

  http://ruby-doc.org/stdlib-2.2.2/libdoc/delegate/rdoc/Object.html <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 <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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20160126/21ca7732/attachment.html>


More information about the Chat mailing list