Robert Klemme
10/23/2006 8:27:00 PM
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