Working with Tempfile class

I've encountered following problems working with Tempfile:

1. Tempfile is not useful for working with binary data on Windows - I guess that this is a problem specific only to native Windows platform. The reason for this is that the underlying file is opened and reopened without explicitly specified binary mode - so in Windows it is opened in TEXT mode. That way binary data undergo newline translation and get corrupted. Here is diff for tempfile.rb with proper patch. I'm not sure if it is enough - it works for me.

---start---
55c55
< @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600)

···

---
> @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL|File::BINARY, 0600)
77c77
< @tmpfile = File.open(@tmpname, 'r+')
---
> @tmpfile = File.open(@tmpname, 'rb+')
---end---

2. Tempfile is not recognized as File - although it is File - isn't it? I think that proper place to resolve this problem is delegate.rb file:

---start---
61a62,65
> alias_method :org_kind_of?, :kind_of?
> def kind_of?(some_class)
> org_kind_of?(some_class) || @_dc_obj.kind_of?(some_class)
> end
137a142,147
> end
> klass.module_eval do
> alias_method :org_kind_of?, :kind_of?
> def kind_of?(some_class)
> org_kind_of?(some_class) || @_dc_obj.kind_of?(some_class)
> end
---end---

This path changes kind_of? behavior so that it compares passed class not only to itself but also to class of basic object. I didn't find tests which could ensure me that these changes wouldn't break anything, so I submit them under your careful consideration.

--
Best regards
RNicz

RNicz wrote:

I've encountered following problems working with Tempfile:

1. Tempfile is not useful for working with binary data on Windows

Tempfile#binmode should solve that problem.

Tobias

If your code is looking for #kind_of?(File) or #kind_of?(IO), it
shouldn't. It won't recognise custom "writeable" or "readable"
objects, e.g., StringIO or even strings that have had a custom #write
or #read method added.

-austin

···

On Sun, 27 Feb 2005 10:34:56 +0900, RNicz <rnicz@fibernet.pl> wrote:

2. Tempfile is not recognized as File - although it is File - isn't it?
I think that proper place to resolve this problem is delegate.rb file:

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

If only there was IO#binmode=...

Binary mode can be set only when opening file. IO#binmode reports IO
stream status.

irb(main):001:0> f=File.new('test', 'w')
=> #<File:test>
irb(main):002:0> f.binmode=true
NoMethodError: undefined method `binmode=' for #<File:test>
        from (irb):2

Tobias Peters wrote:

> 1. Tempfile is not useful for working with binary data on Windows

Tempfile#binmode should solve that problem.

I forgot to mention that I use ruby 1.8.2 i386-mswin32 (One-Click
Installer, v. 1.8.2-14 Final)

···

--
Best regards
RNicz

Oops, it realy works. I'm sorry that I have not checked this well
before posting.

···

--
RNicz