Re: File::open and File.open

From: Brad T.

What exactly is the difference between these?

:: and . are interchangeable for method calls (I think fully):
irb(main):001:0> class Foo; attr_accessor :bar; end
=> nil
irb(main):002:0> f = Foo.new
=> #Foo:0x2c9abd8
irb(main):003:0> f::bar
=> nil
irb(main):004:0> f::bar = 12
=> 12
irb(main):005:0> f::bar
=> 12

“::” must be used for constant lookup:

irb(main):001:0> class Foo; BAR = 12; end
=> 12
irb(main):002:0> Foo::BAR
=> 12
irb(main):003:0> Foo.BAR
NoMethodError: undefined method `BAR’ for Foo:Class
from (irb):3

Most people (I believe) only use “::” for constants, and “.” for all
method calls.

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