Hi --
What's the difference between
class Foo
class << self
def foo; end
end
end
and
class Foo
def self.foo; end
end
In the general case of class << obj; def x, vs. def obj.x, there's a
difference in the scoping of constants:
A = 1
class C
A = 2
end
c = C.new
class << c
def x
puts A # this is C's A
end
end
def c.y
puts A # this is top-level A
end
c.x
c.y
But I don't think this will loom very large in the class-method case.
Any constants you define in the class scope will be visible in both
the methods. There may be some way to squeeze a difference out of
them by defining methods in Class or something... but they're
basically interchangeable.
David
--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1]
http://www.manning.com/black | [3]
http://www.rubypowerandlight.com
[2]
http://dablog.rubypal.com | [4]
http://www.rubycentral.org