Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > "def self.method" vs "class << self; def method"

Reply
Thread Tools

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

 
 
joevandyk@gmail.com
Guest
Posts: n/a
 
      10-09-2006
What's the difference between

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

and

class Foo
def self.foo; end
end

Joe

 
Reply With Quote
 
 
 
 
Alexandru Popescu
Guest
Posts: n/a
 
      10-10-2006
On 10/10/06, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      10-10-2006
Hi --

On Tue, 10 Oct 2006, (E-Mail Removed) wrote:

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

 
Reply With Quote
 
Ken Bloom
Guest
Posts: n/a
 
      10-10-2006
(E-Mail Removed) 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/~kbloom1/
I've added a signing subkey to my GPG key. Please update your keyring.
 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      10-10-2006

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


 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      10-10-2006
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

 
Reply With Quote
 
Max Lapshin
Guest
Posts: n/a
 
      10-10-2006

On Oct 10, 2006, at 3:55 AM, (E-Mail Removed) 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.

 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      10-10-2006
Hi --

On Tue, 10 Oct 2006, Ken Bloom wrote:

> (E-Mail Removed) 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.


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

class C
attr_accessor
# and
class << self
attr_accessor

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
Newbie: def must come before call to def? planetthoughtful Ruby 4 03-12-2007 11:36 AM
No class def found Chris Java 2 01-11-2005 12:43 AM
Is there a way to use "def self.new" to do the job of "def initialize"? Sean Ross Ruby 3 12-25-2003 04:59 AM
HttpModule -- how to intercept urls like http://localhost/abc/def or http://localhost/abc/def/ where abc, def are non virtual dir Jiong Feng ASP .Net 0 11-19-2003 05:29 AM



Advertisments