# Tempfile.rb and unlink on windows

**URL:** <https://www.ruby-forum.com/t/tempfile-rb-and-unlink-on-windows/168324>\
**Category:** Ruby\
**Created:** [May 19, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/tempfile-rb-and-unlink-on-windows/168324 "2009-05-19T17:01:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![modethree3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@modethree3](https://www.ruby-forum.com/u/modethree3)\
**Post date:** [May 19, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/tempfile-rb-and-unlink-on-windows/168324/1 "2009-05-19T17:01:25Z")

</div>

PROBLEM

Was setting up a rails projects on a windows machine for and the project  
required RubyInline. When running rake db:migrate for the first time  
RubyInline creates a temp file to ensure it can have access to something  
like /tmp/ruby\_inline but causes an error:

rake aborted!  
File exists - /tmp/ruby\_inline3712-0

The problem here is that on windows, doing something like (in order to  
emulate this problem)

ruby -r tempfile -e ‘Tempfile.new(“foo”).unlink’

Will _not_ work on Windows (XP Pro).

COMMENTS

Whoever freakin wrote Tempfile#unlink had this little gem in the code:  
rescue Errno::EACCES  
# may not be able to unlink on Windows; just ignore  
So if it can’t delete (unlink) the file it just silently fails - not  
cool.

SOLUTION

After reading this post:  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/2848](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/2848) I  
applied the following patch and it worked finally (thanks to Florian  
Frank who wrote it).

— lib/tempfile.rb 23 Jul 2003 16:37:35 -0000 1.19  
+++ lib/tempfile.rb 5 May 2004 23:33:57 -0000  
@@ -106,7 +106,10 @@ class Tempfile \< SimpleDelegator

# file.

def unlink  
# keep this order for thread safeness

- File.unlink(@tmpname) if File.exist?(@tmpname)

- if File.exist?(@tmpname)
- 

```
 closed? or close

```

- 

```
 File.unlink(@tmpname)

```

- end  
@@cleanlist.delete(@tmpname) if @@cleanlist  
end  
alias delete unlink

Can anyone provide any insight on this?

---

<div class="post-metadata">

**Author:** ![modethree3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@modethree3](https://www.ruby-forum.com/u/modethree3)\
**Post date:** [May 19, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/tempfile-rb-and-unlink-on-windows/168324/2 "2009-05-19T18:32:08Z")

</div>

> required RubyInline. When running rake db:migrate for the first time  
> ruby -r tempfile -e ‘Tempfile.new(“foo”).unlink’
> 
> # file.
> 
> Can anyone provide any insight on this?

Your patch looks good to me. I would submit it on their redmine site:

> **[Ruby Issue Tracking System](https://redmine.ruby-lang.org)**
>
> Redmine

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![modethree3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@modethree3](https://www.ruby-forum.com/u/modethree3)\
**Post date:** [May 19, 2009, 9:16pm UTC](https://www.ruby-forum.com/t/tempfile-rb-and-unlink-on-windows/168324/3 "2009-05-19T21:16:33Z")

</div>

Daniel B. wrote:

> > required RubyInline. When running rake db:migrate for the first time  
> > ruby -r tempfile -e ‘Tempfile.new(“foo”).unlink’
> > 
> > # file.
> > 
> > Can anyone provide any insight on this?
> 
> Your patch looks good to me. I would submit it on their redmine site:
> 
> [http://redmine.ruby-lang.org](http://redmine.ruby-lang.org)
> 
> Regards,
> 
> Dan

Yeah I will get around to it but it’s just f\*cked up that the rescue  
clause just silently fails and says in a comment “oh may fail on windows  
but who cares” lol
