[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Joe Van Dyk

10/9/2006 11:52:00 PM

What's the difference between

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

and

class Foo
def self.foo; end
end

Joe

8 Answers

Alexandru Popescu

10/10/2006 12:20:00 AM

0

On 10/10/06, joevandyk@gmail.com <joevandyk@gmail.com> wrote:
> 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
--
w( the_mindstorm )p.

dblack

10/10/2006 1:25:00 AM

0

Ken Bloom

10/10/2006 1:26:00 AM

0

joevandyk@gmail.com wrote:
> 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

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...
I've added a signing subkey to my GPG key. Please update your keyring.

Jeff

10/10/2006 3:28:00 AM

0


Ken Bloom 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


Gavin Kistner

10/10/2006 4:20:00 AM

0

Jeff wrote:
> Ken Bloom 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

???? ??????

10/10/2006 4:56:00 AM

0


On Oct 10, 2006, at 3:55 AM, joevandyk@gmail.com 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.

dblack

10/10/2006 8:47:00 AM

0

Ken Bloom

10/10/2006 3:34:00 PM

0

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

> Ken Bloom 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

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...
I've added a signing subkey to my GPG key. Please update your keyring.