File.open options hash "mode" as int

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-

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.

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…

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).

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:

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:–

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.

On Thu, May 19, 2011 at 7:41 PM, 7stud – [email protected]alid
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


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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs