# File.open options hash "mode" as int

**URL:** <https://www.ruby-forum.com/t/file-open-options-hash-mode-as-int/207075>\
**Category:** Ruby\
**Created:** [May 17, 2011, 8:22pm UTC](https://www.ruby-forum.com/t/file-open-options-hash-mode-as-int/207075 "2011-05-17T20:22:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [May 17, 2011, 8:22pm UTC](https://www.ruby-forum.com/t/file-open-options-hash-mode-as-int/207075/1 "2011-05-17T20:22:37Z")

</div>

Hello.  
in 1.9.x, I see that these work:  
File.open(‘yo’, ‘w’)  
File.open(‘yo’, File::WRONLY|File::TRUNC|File::CREAT)  
File.open(‘yo’, :mode =\> ‘w’)

So shouldn’t this also work?

> > File.open(‘yo’, :mode =\> File::WRONLY|File::TRUNC|File::CREAT)  
> > TypeError: can’t convert Fixnum into String

-roger-

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [May 17, 2011, 8:49pm UTC](https://www.ruby-forum.com/t/file-open-options-hash-mode-as-int/207075/2 "2011-05-17T20:49:09Z")

</div>

Well, the docs say this:

===  
IO.open(fd, mode\_string=“r” [, opt] ) → io  
IO.open(fd, mode\_string=“r” [, opt] ) {|io| block } → obj

Document-method: IO::open

# With no associated block, open is a synonym for IO.new.

===  
IO.new(fd [, mode] [, opt]) → io

Returns a new IO object (a stream) for the given IO object or integer  
file descriptor and mode string. See also IO.sysopen and IO.for\_fd.

# Parameters fd: numeric file descriptor mode: file mode. a string or an integer opt: hash for specifying mode by name.

The last line there seems to explain why you are getting the error.  
Although, you aren’t supplying a _numeric_ file descriptor either, so  
who knows.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [May 17, 2011, 9:28pm UTC](https://www.ruby-forum.com/t/file-open-options-hash-mode-as-int/207075/3 "2011-05-17T21:28:13Z")

</div>

> mode: file mode. a string or an integer

> The last line there seems to explain why you are getting the error.  
> Although, you aren’t supplying a _numeric_ file descriptor either, so  
> who knows.

File::WRONLY|File::TRUNC|File::CREAT is numeric…

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [May 17, 2011, 11:43pm UTC](https://www.ruby-forum.com/t/file-open-options-hash-mode-as-int/207075/4 "2011-05-17T23:43:09Z")

</div>

Roger P. wrote in post #999326:

> > mode: file mode. a string or an integer
> 
> > The last line there seems to explain why you are getting the error.  
> > Although, you aren’t supplying a _numeric_ file descriptor either, so  
> > who knows.
> 
> File::WRONLY|File::TRUNC|File::CREAT is numeric…

1. That isn’t a “file descriptor”, and
2. What does the last line of the docs I posted say?

opt: hash for specifying mode _by name_.

Or, are you having trouble with the distribution of arguments to the  
method parameters:

def do\_stuff(a, b=“hello”, c)  
p c  
end

do\_stuff(10, :x =\> ‘goodbye’)

–output:–  
{:x=\>“goodbye”}

This comment:

> Although, you aren’t supplying a _numeric_ file descriptor either,  
> so who knows.

points out that according to the docs, you can’t supply a string as the  
first argument, which isn’t true. Therefore, when the docs say that the  
‘opt’ parameter must be a hash with a key called :mode, which specifies  
a name (not an integer), the docs may be wrong about that too(although  
your error would indicate otherwise).

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [May 19, 2011, 7:41pm UTC](https://www.ruby-forum.com/t/file-open-options-hash-mode-as-int/207075/5 "2011-05-19T19:41:23Z")

</div>

Don’t get me started on how poor the ruby docs are. I think this line:

===  
IO.open(fd, mode\_string=“r” [, opt] ) → io  
IO.open(fd, mode\_string=“r” [, opt] ) {|io| block } → obj

# With no associated block, open is a synonym for IO.new.

should say ‘File.new’ instead of ‘IO.new’. And, I don’t understand why  
every other method in the File docs is specified as:

File.meth\_name(…)

but for open() it’s specified as

IO.open(..)

That might be an attempt to indicate that open() is inherited and not  
actually defined in File, but if you look at Programming Ruby here:

> **[Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/)**

It appears that File does override IO.open in order to accept a string  
for the filename. But look at this:

puts File.singleton\_class.public\_instance\_methods.grep(/^op/)

–output:–  
open

not\_inherited = false  
puts  
File.singleton\_class.public\_instance\_methods(not\_inherited).grep(/^op/)

–output:–

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [May 19, 2011, 5:12pm UTC](https://www.ruby-forum.com/t/file-open-options-hash-mode-as-int/207075/6 "2011-05-19T17:12:57Z")

</div>

> opt: hash for specifying mode by name.

I guess this is why. I just don’t see quite _why_ it requires mode by  
name in opts, but accepts it by int when given as a parameter. But that  
makes sense.

A few more notes.

> IO.open(fd, mode\_string=“r” [, opt] ) → io

Yeah, interestingly, this doesn’t look at all like  
File.open(‘filename’)  
so…the docs just lack the most common usage of File.open apparently?  
Odd.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [May 19, 2011, 10:17pm UTC](https://www.ruby-forum.com/t/file-open-options-hash-mode-as-int/207075/7 "2011-05-19T22:17:34Z")

</div>

On Thu, May 19, 2011 at 7:41 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Don’t get me started on how piss poor the ruby docs are.

[http://blog.steveklabnik.com/2011/05/10/contributing-to-ruby-s-documentation.html](http://blog.steveklabnik.com/2011/05/10/contributing-to-ruby-s-documentation.html)

–  
Phillip G.

A method of solution is perfect if we can forsee from the start,  
and even prove, that following that method we shall attain our aim.  
– Leibnitz
