[LRUG] Understanding Ruby Tempfile documentation

Craig R Webster craig at xeriom.net
Tue Jan 26 05:10:14 PST 2016


Hello,

If you did it this way Tempfile would no longer have the same interface as File. The constructor signature has changed so I think you’d be violating the Liskov substitution principle. If Tempfile IS-A File then I should be able to use it in place of File in any code that uses File.

There’s quite a lot in the inheritance vs composition debate, and some of it is quite subtle - I don’t feel knowledgable enough to cover much of it. There’s a brief overview on Wikipedia to get started with: https://en.wikipedia.org/wiki/Composition_over_inheritance#Benefits

Cheers,
Craig

> On 26 Jan 2016, at 12:39, Jonathon Horsman <jonathon at arctickiwi.com> wrote:
> 
> 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 <mailto: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 <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 <mailto: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 <mailto:Chat at lists.lrug.org>
>> Archives: http://lists.lrug.org/pipermail/chat-lrug.org <http://lists.lrug.org/pipermail/chat-lrug.org>
>> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org <http://lists.lrug.org/options.cgi/chat-lrug.org>
>> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org <http://lists.lrug.org/listinfo.cgi/chat-lrug.org>
> 
> 
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org <mailto:Chat at lists.lrug.org>
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org <http://lists.lrug.org/pipermail/chat-lrug.org>
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org <http://lists.lrug.org/options.cgi/chat-lrug.org>
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org <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/7509edb2/attachment-0002.html>


More information about the Chat mailing list