3

I'm trying to debug/diagnose some strange behaviour, and hoping someone can have some insight for me. This is in Ruby 1.9.3.

We've got some code that opens an uploaded file to determine its MIME type, which boils down to:

open(file) { |f| get_mime_type(f) }

Pretty straightforward. In this case, file is actually a File object (or a Rack::Test::UploadedFile in our test suite), not a path, but open seems to work fine with a file object.

... Except that we now have a new member of the team and it's not working for him. His environment is set up for the most part the same way (anything relevant I could think of is identical - ruby version and patchlevel, rails version, installed gems), but on his machine, when a file object is passed to open, it returns a file object and ignores the block altogether. Passing in a path instead of a file object, however, works:

open(file.path) { |f| get_mime_type(f) }

So that's our temporary fix, but what I'm trying to figure out is why this is happening? I'd appreciate any insight!

4

1 回答 1

1

我想弄清楚为什么会这样?

的第一个参数Kernel#open应该File对象一起使用,而是与String 表示路径的对象一起使用。它适用于某些机器的事实并不能证明它是有效的或建议的使用方式open

您绝对应该继续使用:

open(file.path) { |f| get_mime_type(f) }
于 2013-07-18T18:24:20.440 回答