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

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

  1. joevandyk

    joevandyk Guest

    What's the difference between

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

    and

    class Foo
    def self.foo; end
    end

    Joe
     
    joevandyk, Oct 10, 2006
    #1
    1. Advertisements

  2. 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
     
    Alexandru Popescu, Oct 10, 2006
    #2
    1. Advertisements

  3. joevandyk

    dblack Guest

    Hi --

    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
     
    dblack, Oct 10, 2006
    #3
  4. joevandyk

    Ken Bloom Guest

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

    --Ken
     
    Ken Bloom, Oct 10, 2006
    #4
  5. joevandyk

    Jeff Guest

    Can you elaborate a little bit more?

    Jeff
     
    Jeff, Oct 10, 2006
    #5
  6. joevandyk

    Phrogz Guest

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

    Max Lapshin Guest

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

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

    dblack Guest

    Hi --

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

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 (here). After that, you can post your question and our members will help you out.