Working with Tempfile class

R

RNicz

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 :eek:rg_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 :eek:rg_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.
 
T

Tobias Peters

RNicz said:
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
 
A

Austin Ziegler

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:

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
 
R

RNicz

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 said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top