[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

File::open and File.open

Brad Tilley

10/23/2006 8:18:00 PM

What exactly is the difference between these? :: is a class method while
is an instance method??? If that is so, could someone clarify this
with more detail? I use File.open, but File::open works too. However
sometimes, this is not the case which adds to my confusion :) For
example, I can only do Digest::MD5.new() and not Digest.MD5.new() why is
that?

I don't fully understand OO programming... perhaps this is my problem.

Thank you for your time.

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

3 Answers

Robert Klemme

10/23/2006 8:27:00 PM

0

Brad Tilley wrote:
> What exactly is the difference between these? :: is a class method while
> is an instance method??? If that is so, could someone clarify this
> with more detail? I use File.open, but File::open works too.

The same method (a class method) is called. It is just a syntactic
difference. So these are the same

File.open("foo") ...
File::open("foo") ...

> However
> sometimes, this is not the case which adds to my confusion :) For
> example, I can only do Digest::MD5.new() and not Digest.MD5.new() why is
> that?

Because the "." cannot be used for constant lookup:

irb(main):005:0> File.NONBLOCK
NoMethodError: undefined method `NONBLOCK' for File:Class
from (irb):5
from :0
irb(main):006:0> File::NONBLOCK
=> 16384

"." is reserved for method invocations.

> I don't fully understand OO programming... perhaps this is my problem.

Nah, I think it is rather a minor ambiguity in the language that causes
confusion. Usually people disambiguate it by using "." for methods and
"::" for constants only although they could use it for class methods as
well.

Kind regards

robert

Brad Tilley

10/23/2006 8:36:00 PM

0

Thank you Robert and Jason. I understand that much better now!


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

Rick DeNatale

10/24/2006 9:34:00 PM

0

On 10/23/06, Jason Roelofs <jameskilton@gmail.com> wrote:
> This is just Ruby syntax. It allows both :: and . as operators to
> class-level methods and variables.
>
> File::open and File.open are the exact same thing: a class method. Were it
> an instance method, you would have to do:
>
> file = File.new
> file.open

And when talking about instance methods one sometimes uses

classname#method name

Since this is what ri understands when you ask for documentation:

$ri to_s
More than one method matched your request. You can refine
your search by asking for information on one of:

Method#to_s, Vector#to_s, Struct#to_s, Time#to_s, CGI::Cookie#to_s,
Array#to_s, Matrix#to_s, MatchData#to_s, FalseClass#to_s,
Pathname#to_s, Pathname#to_str, Rational#to_s, Hash#to_s,
UnboundMethod#to_s, TrueClass#to_s, Module#to_s, Complex#to_s,
Proc#to_s, Symbol#to_s, Symbol#to_sym, Exception#to_s,
Exception#to_str, Bignum#to_s, Object#to_s, NilClass#to_s,
Range#to_s, Date#to_s, NameError#to_s, Fixnum#to_s, Fixnum#to_sym,
Float#to_s, String#to_s, String#to_str, String#to_sym, Regexp#to_s,
Benchmark::Tms#to_s, URI::MailTo#to_s, URI::FTP#to_s,
URI::Generic#to_s, Process::Status#to_s, Enumerable#to_set

$ri Object#to_s
------------------------------------------------------------ Object#to_s
obj.to_s => string
------------------------------------------------------------------------
Returns a string representing _obj_. The default +to_s+ prints the
object's class and an encoding of the object id. As a special case,
the top-level object that is the initial execution context of Ruby
programs returns ``main.''

And, by the way, the open method in question is really a class method
of IO rather than File, the File class inherits it.



--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...