Forum: Ruby File.open options hash "mode" as int

Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2011-05-17 20:22
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-
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2011-05-17 20:49
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.
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2011-05-17 21:28
> 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...
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2011-05-17 23:43
Roger Pack 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).
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2011-05-19 17:12
> 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.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2011-05-19 19:41
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:--
<nothing>
Ac0085dae0703db56ad7f8cb9e1798ba?d=identicon&s=25 Phillip Gawlowski (Guest)
on 2011-05-19 22:17
(Received via mailing list)
On Thu, May 19, 2011 at 7:41 PM, 7stud -- <bbxx789_05ss@yahoo.com>
wrote:
>
> Don't get me started on how piss poor the ruby docs are.

http://blog.steveklabnik.com/2011/05/10/contributi...

--
Phillip Gawlowski

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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.