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

What’s the difference between

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

and

class Foo
def self.foo; end
end

Joe

On 10/10/06, [email protected] [email protected] wrote:

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

Hi –

On Tue, 10 Oct 2006, [email protected] wrote:

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

Ken B. wrote:

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

Can you elaborate a little bit more?

Jeff

[email protected] wrote:

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

Jeff wrote:

Ken B. wrote:

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

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

On Oct 10, 2006, at 3:55 AM, [email protected] wrote:

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.

Hi –

On Tue, 10 Oct 2006, Ken B. wrote:

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.

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

On Tue, 10 Oct 2006 12:28:02 +0900, Jeff wrote:

Ken B. wrote:

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

Can you elaborate a little bit more?

Jeff

There’s no analagous syntax to using
def self.foo; end
for the attr_accessor method (and the like).

There, you must use
class << self; attr_accessor :foo; end

–Ken

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