File::open and File.open

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 :slight_smile: 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.

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

aka, create an instance of the object, then call the function. For
confusions sake, this is the same as:

file = File::new
file.open

Jason

Brad T. 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 :slight_smile: 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

Robert K. schrieb:

    from :0

well.
I was bitten by it more than once
is there more to it … i mean ambiguity is usually not what one wants
to have in the language (at least not in the computer language :slight_smile:

Regards, Daniel

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

On 10/23/06, Jason R. [email protected] 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.denhaven2.com/

IPMS/USA Region 12 Coordinator
http://ipmsr12.denhaven2.com/

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/

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