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

Discussion in 'Ruby' started by joevandyk@gmail.com, Oct 10, 2006.

  1. Guest

    What's the difference between

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

    and

    class Foo
    def self.foo; end
    end

    Joe
     
    , Oct 10, 2006
    #1
    1. Advertising

  2. On 10/10/06, <> 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.
     
    Alexandru Popescu, Oct 10, 2006
    #2
    1. Advertising

  3. Guest

    Hi --

    On Tue, 10 Oct 2006, 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 |
    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
     
    , Oct 10, 2006
    #3
  4. Ken Bloom Guest

    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.
     
    Ken Bloom, Oct 10, 2006
    #4
  5. Jeff Guest

    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
     
    Jeff, Oct 10, 2006
    #5
  6. Phrogz Guest

    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
     
    Phrogz, Oct 10, 2006
    #6
  7. Max Lapshin Guest

    On Oct 10, 2006, at 3:55 AM, 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.
     
    Max Lapshin, Oct 10, 2006
    #7
  8. Guest

    Hi --

    On Tue, 10 Oct 2006, Ken Bloom wrote:

    > 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 :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 |
    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
     
    , Oct 10, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page