[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Extremely Noobish Documentation Question

Paco

11/26/2006 9:53:00 PM

Hello all,

I am a Java programmer and am attempting to learn Ruby. What I have
seen so far, I really like. I have been through two of the tutorials
linked to from the main site and read the Poignant Guide to Ruby.
However, when I refer to the actual documentation, I find it lacking ...
or maybe I'm just confused. In many examples I have seen File.open used
but when I refer to the official documentation
(http://www.ruby-doc...) the 'open' method is not listed for the
File class.

After doing some digging, I think I understand that it is some kind of
alias to 'new' but how would I know that (if I'm even right). Also, I
think File inherits from IO but I can't see in the documentation where
this is indicated. Am I just looking at it all wrong? Am I looking in
the wrong place? Basically, I like the language but the documentation I
have seen appears to be lacking compared to the Java API. Any help
would be greatly appreciated.

Thanks,
Paco

--
Posted via http://www.ruby-....

15 Answers

Li Chen

11/26/2006 10:04:00 PM

0

Patrick McNally wrote:
In many examples I have seen File.open used
> but when I refer to the official documentation
> (http://www.ruby-doc...) the 'open' method is not listed for the
> File class.
>> Paco


Hi,

open methed is a method defined in kernel module. Open a window prompt
and try the following

C:\>ri open
More than one method matched your request. You can refine
your search by asking for information on one of:

CSV::open, CSV::open_reader, CSV::open_writer, Dir::open,
...
Iconv::open, IO::open, IO::popen, IO::sysopen, IO#reopen,
Kernel#open, Kernel#open_uri_original_open,
... Win32::EventLog::open_backup




Form the screen you can see kernel#open. This is where open comes from.
BTW Ruby is case sensitive.

After that you can try the follows

C:\>ri Kernel.open
------------------------------------------------------------ Kernel#open
open(path [, mode [, perm]] ) => io or nil
open(path [, mode [, perm]] ) {|io| block } => obj
------------------------------------------------------------------------
Creates an +IO+ object connected to the given stream, file, or
subprocess.

....
open("testfile") do |f|
print f.gets
end

_produces:_

This is line one

Open a subprocess and read its output:

cmd = open("|date")
print cmd.gets
cmd.close

_produces:_

Wed Apr 9 08:56:31 CDT 2003

-- More --

...




Li




--
Posted via http://www.ruby-....

Paco

11/26/2006 10:19:00 PM

0

> open methed is a method defined in kernel module

Thank you for the quick response. I see that now but I guess my
underlying questions still remains. What should indicate that the
method is part of Kernel and why is it invoked when I call 'File.open'?
I apologize if I am asking these questions in the wrong place. I'm just
feeling a little lost.

If there is a more comprehensive resource that I should read, let me
know. I hate to be asking questions that have answers elsewhere, but
I've found that most of the documentation I have seen does not answer
the questions I have.

-Paco

--
Posted via http://www.ruby-....

Tim Hunter

11/26/2006 10:32:00 PM

0

Patrick McNally wrote:
>> open methed is a method defined in kernel module
>>
>
> Thank you for the quick response. I see that now but I guess my
> underlying questions still remains. What should indicate that the
> method is part of Kernel and why is it invoked when I call 'File.open'?
> I apologize if I am asking these questions in the wrong place. I'm just
> feeling a little lost.
>
> If there is a more comprehensive resource that I should read, let me
> know. I hate to be asking questions that have answers elsewhere, but
> I've found that most of the documentation I have seen does not answer
> the questions I have.
>
> -Paco
>
>
Ruby's doc is not as comprehensive as Java's. That's all there is to it.
There's also 100 million Java books but only 2 dozen or so for Ruby.
You'll catch up as you go along. In addition to ruby-doc, I recommend
you get a copy of Dave Thomas' _Programming_Ruby_. If you really get
into Ruby there's a couple other good books. Consider Hal Fulton's
_The_Ruby_Way_, David Black's _Ruby_for_Rails_, or the _Ruby_Cookbook_
(can't remember the authors' names).

Li Chen

11/26/2006 10:44:00 PM

0

Patrick McNally wrote:
>> open methed is a method defined in kernel module
>
> Thank you for the quick response. I see that now but I guess my
> underlying questions still remains. What should indicate that the
> method is part of Kernel and why is it invoked when I call 'File.open'?
> I apologize if I am asking these questions in the wrong place. I'm just
> feeling a little lost.
>
> If there is a more comprehensive resource that I should read, let me
> know. I hate to be asking questions that have answers elsewhere, but
> I've found that most of the documentation I have seen does not answer
> the questions I have.
>
> -Paco

check this out
http://dev.rubycentra...

or Pickaxe 1st edition(online version)
http://www.ruby-doc.org/docs/Progra...

But the better means is to buy a Pickaxe 2nd edition(Dave Thomas'
Programming Ruby) and read it on the bed when you feel there is nothing
else to do.

Li


--
Posted via http://www.ruby-....

Paul Lutus

11/26/2006 10:56:00 PM

0

Patrick McNally wrote:

>> open methed is a method defined in kernel module
>
> Thank you for the quick response. I see that now but I guess my
> underlying questions still remains. What should indicate that the
> method is part of Kernel and why is it invoked when I call 'File.open'?

Because the File class inherits all the methods from the Kernel class:

p File.ancestors

[File, IO, File::Constants, Enumerable, Object, Kernel]


Like any other class, File has access to the methods of its ancestors.

--
Paul Lutus
http://www.ara...

Olivier

11/26/2006 10:59:00 PM

0

Le dimanche 26 novembre 2006 22:53, Paco Paco a écrit :
> ...
> but when I refer to the official documentation
> (http://www.ruby-doc...) the 'open' method is not listed for the
> File class.
> ...

You did everything well : ruby-doc.org is the place to find the api, and you
should have found exactly what you were looking for (eg. the File.open class
method), in normal time...

But for some reasons, it happens that the generated doc gets bloated by
classes that should not be there, in the core api. Maybe a mistake from
someone, maybe a technical problem, perhaps someone in the ML knows what
happens ?
In any case, this is annoying : in your case, the original File class api was
replaced by another one, which is useless to you.

You can use ri instead, but it's not as simple as ruby-doc. And I don't know
another place to find this api. Another solution is to generate the api
yourself with rdoc.

Florian Frank

11/26/2006 11:05:00 PM

0

Patrick McNally schrieb:
>> open methed is a method defined in kernel module
>>
>
> Thank you for the quick response.
This answer was not correct. You cannot find the File.open method,
because there is none. It is inherited from the IO class, because File <
IO == true. If you want to find out where a method is located you can
type this into irb:
>> File.method(:open)
# => #<Method: File(IO).open>

This shows the inheritance relation. Ruby's rdoc/ri is a bit stupid, and
can't locate inherited methods so well.

There is also a Kernel#open method, that is mixed into the Object class
as a private method. This makes it possible to open a file by calling
open(filename) do |file|
#...
end
from everywhere. If you call File.open this isn't the method called,
because IO#open is found first:

>> require 'pp'; pp File.ancestors.map { |klass| [ klass,
klass.method(:open) ] }
[[File, #<Method: File(IO).open>],
[IO, #<Method: IO.open>],
[File::Constants, #<Method: Module(Kernel)#open>],
[Enumerable, #<Method: Module(Kernel)#open>],
[Object, #<Method: Class(Kernel)#open>],
[PP::ObjectMixin, #<Method: Module(Kernel)#open>],
[Kernel, #<Method: Kernel.open>]]

You can call Kernel#open only without an explicit receiver, because it
is private. That's why calling, e. g., Array.open would fail with a
NoMethodError.

--
Florian Frank

matt

11/26/2006 11:09:00 PM

0

Paco Paco <mepaco@gmail.com> wrote:

> In many examples I have seen File.open used
> but when I refer to the official documentation
> (http://www.ruby-doc...) the 'open' method is not listed for the
> File class.

Isn't the problem here that the File class inherits from the IO class?
IO has an open method, therefore File has an open method. But you have
to look under IO to see it.

It might be helpful to have on hand a script that reports a class's
methods by cycling up the inheritance chain for you:

def method_report(klass)
result = klass.ancestors.inject(Array.new) do |result, anc|
ms = (anc.instance_methods + anc.methods).sort.uniq
result.last[1] -= ms if result.last
result << [anc.name, ms]
end
result.each do |k, v|
puts "----", k
v.each {|m| puts "\s\s#{m}"}
end
end
# and here's how to use it
method_report(File)

m.
--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Ross Bamford

11/26/2006 11:18:00 PM

0

On Sun, 26 Nov 2006 23:09:07 -0000, matt neuburg <matt@tidbits.com> wrot=
e:

> It might be helpful to have on hand a script that reports a class's
> methods by cycling up the inheritance chain for you:
>

Or, get fastri:

$ fri File.open
--------------------------------------------------------------- IO::open=

IO.open(fd, mode_string=3D"r" ) =3D> io
IO.open(fd, mode_string=3D"r" ) {|io| block } =3D> obj
------------------------------------------------------------------------=

With no associated block, open is a synonym for IO::new. If the
optional code block is given, it will be passed io as an argument,=

and the IO object will automatically be closed when the block
terminates. In this instance, IO::open returns the value of the
block.



It handles the inheritance stuff much better than RI.
See http://eigenclass.org/hiki.rb?fa...

-- =

Ross Bamford - rosco@roscopeco.remove.co.uk

Edwin Fine

11/26/2006 11:28:00 PM

0

Timothy Hunter wrote:
> Patrick McNally wrote:
>> know. I hate to be asking questions that have answers elsewhere, but
>> I've found that most of the documentation I have seen does not answer
>> the questions I have.
>>
>> -Paco
>>
>>
> Ruby's doc is not as comprehensive as Java's. That's all there is to it.
> There's also 100 million Java books but only 2 dozen or so for Ruby.
> You'll catch up as you go along. In addition to ruby-doc, I recommend
> you get a copy of Dave Thomas' _Programming_Ruby_. If you really get
> into Ruby there's a couple other good books. Consider Hal Fulton's
> _The_Ruby_Way_, David Black's _Ruby_for_Rails_, or the _Ruby_Cookbook_
> (can't remember the authors' names).

Ruby Cookbook, by Lucas Carlson and Leonard Richardson (O'Reiily).
*Highly* recommended, after you finish Pickaxe II.

--
Posted via http://www.ruby-....