[LRUG] Understanding Ruby Tempfile documentation

Artan Sinani artisinani at gmail.com
Tue Jan 26 05:07:30 PST 2016


>From Pickaxe book:

" 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.

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.

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. "



On 26 January 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> 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
>>
>>
>
> _______________________________________________
> 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/cc48e510/attachment-0002.html>


More information about the Chat mailing list