self in method name

Discussion in 'Ruby' started by iskaldur, Jun 12, 2007.

  1. iskaldur

    iskaldur Guest

    Is there any difference between using self in a method name, and not
    using it?
    For example, I've seen a lot something like

    class Foo
    def self.bar
    ...
    end
    end

    Is this the same thing as

    class Foo
    def bar
    ...
    end
    end
    ?

    --
    Posted via http://www.ruby-forum.com/.
     
    iskaldur, Jun 12, 2007
    #1
    1. Advertising

  2. The first one creates a class method as opposed to the second one that
    creates an instance method (e.g., Foo.bar rather than x = Foo.new;
    x.bar;). :)

    --Jeremy

    On 6/12/07, iskaldur <> wrote:
    > Is there any difference between using self in a method name, and not
    > using it?
    > For example, I've seen a lot something like
    >
    > class Foo
    > def self.bar
    > ...
    > end
    > end
    >
    > Is this the same thing as
    >
    > class Foo
    > def bar
    > ...
    > end
    > end
    > ?
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    http://www.jeremymcanally.com/

    My free Ruby e-book:
    http://www.humblelittlerubybook.com/book/

    My blogs:
    http://www.mrneighborly.com/
    http://www.rubyinpractice.com/
     
    Jeremy McAnally, Jun 12, 2007
    #2
    1. Advertising

  3. iskaldur

    Guest

    Hi --

    On Tue, 12 Jun 2007, iskaldur wrote:

    > Is there any difference between using self in a method name, and not
    > using it?
    > For example, I've seen a lot something like
    >
    > class Foo
    > def self.bar
    > ...
    > end
    > end
    >
    > Is this the same thing as
    >
    > class Foo
    > def bar
    > ...
    > end
    > end


    def self.bar is just a special case of:

    def some_object.some_method

    i.e., defining a singleton method on an object (a method that exists
    only in the method look-up for that particular object, not for other
    objects of its class). Actually it's not even a special case; it's
    just using the current value of self as the object on which to define
    the method.

    At the top level of a class definition block, self is the class object
    -- in this case, Foo. A singleton method defined on a class object
    (such as Foo.bar) is usually referred to as a class method. In
    addition, class methods are special-cased a bit, in comparison with
    other singleton methods... but that's Lesson Two :)


    David

    --
    * Books:
    RAILS ROUTING (new! http://safari.awprofessional.com/9780321509246)
    RUBY FOR RAILS (http://www.manning.com/black)
    * Ruby/Rails training
    & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
     
    , Jun 12, 2007
    #3
  4. Hi,

    Am Dienstag, 12. Jun 2007, 13:19:02 +0900 schrieb iskaldur:
    >
    > class Foo
    > def self.bar
    > ...
    > end
    > end
    >
    > Is this the same thing as
    >

    class Foo
    class <<self
    def bar
    ...
    end
    end
    end


    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Jun 12, 2007
    #4
  5. iskaldur

    Peter Cooper Guest

    On 6/12/07, Bertram Scharpf <> wrote:
    > > class Foo
    > > def self.bar
    > > ...
    > > end
    > > end

    >
    > Is this the same thing as
    >
    > class Foo
    > class <<self
    > def bar
    > ...
    > end
    > end
    > end


    Yes, except in the second you're actually opening up the virtual class
    to work on it directly whereas in the first example you're defining
    the method using an absolute name.

    It's easy to see this as a pattern though without understanding the
    background, which, hopefully, a brief example will rectify:

    class << Fixnum
    def x; "y"; end
    end

    puts Fixnum.x # => "y"
    puts 10.x # ERROR

    Note that you can dig into virtual classes at any time, not just
    within the capacity of a class you're currently defining :)

    So.. when you see class << self within the definition of another
    class, then it's the equivalent of class << ClassName, and merely
    using the "self" to provide the current class rather than naming it
    explicitly.

    Cheers,
    Peter Cooper
    http://www.rubyinside.com/
     
    Peter Cooper, Jun 12, 2007
    #5
  6. > class Foo
    > def self.bar
    > end
    >
    > def bar
    > end
    > end


    Arguably, the equivalent in Java is this:

    public class Foo {
    public static void bar1() {
    }

    public void bar2() {
    }
    }

    Notice that you can't use the same method name statically and
    as instance method in Java, whereas you can use the same name
    for the class method and the instance method in Ruby.

    gegroet,
    Erik V. - http://www.erikveen.dds.nl/
     
    Erik Veenstra, Jun 12, 2007
    #6
    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.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    601
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    606
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    414
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    385
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    413
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page