"def self.method" vs "class << self; def method"

J

joevandyk

What's the difference between

class Foo
class << self
def foo; end
end
end

and

class Foo
def self.foo; end
end

Joe
 
A

Alexandru Popescu

What's the difference between

class Foo
class << self
def foo; end
end
end

and

class Foo
def self.foo; end
end

Joe

As far as my understanding goes there is none. The rubies will
probably use the first one when they are defining more than one class
methods and this would save typing 5 * (methods - 1) chars :).

/alex
 
D

dblack

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
 
K

Ken Bloom

What's the difference between

class Foo
class << self
def foo; end
end
end

and

class Foo
def self.foo; end
end

AFAIK nothing. But you can't do the second one with most
metaprogrammed methods, for example attr_accessor.

--Ken
 
P

Phrogz

Jeff said:
Can you elaborate a little bit more?

irb(main):001:0> class C; attr_accessor :foo; end
irb(main):002:0> C.foo
NoMethodError: undefined method `foo' for C:Class
from (irb):2
from :0
irb(main):003:0> C.new.foo
=> nil

irb(main):004:0> class C; class << self; attr_accessor :bar; end; end
irb(main):005:0> C.bar
=> nil
 
M

Max Lapshin

What's the difference between

class Foo
class << self
def foo; end
end
end

Such definition evals area of code in scope of object self,
referenced to constant Foo and adds
singleton methods to it.

class Foo
def self.foo; end
end

Such definition creates a singleton method on object self, which is
equal to constant Foo in this scope.
 
D

dblack

Hi --

AFAIK nothing. But you can't do the second one with most
metaprogrammed methods, for example attr_accessor.

That's a different matter, though (if I'm understanding your point
correctly); the difference between:

class C
attr_accessor :x
# and
class << self
attr_accessor :x

is not a limitation or exception -- it's just that you're calling
attr_accessor on two different objects (C and C's singleton class).
The first attr_accessor doesn't write methods in the singleton class,
so it's not analogous to def self.x.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top