[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

self in method name

iskaldur

6/12/2007 4:19:00 AM

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

6 Answers

Michael Hollins

6/12/2007 4:44:00 AM

0

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

I believe the first is equivalent to:

class Foo
def Foo.bar
...
end
end

and so quite different to the second.

Jeremy McAnally

6/12/2007 5:22:00 AM

0

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 <iskaldur@gmail.com> 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-....
>
>


--
http://www.jeremymca...

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

My blogs:
http://www.mrneigh...
http://www.rubyinpra...

dblack

6/12/2007 10:46:00 AM

0

Bertram Scharpf

6/12/2007 11:09:00 AM

0

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

Peter Cooper

6/12/2007 11:18:00 AM

0

On 6/12/07, Bertram Scharpf <lists@bertram-scharpf.de> 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.rubyi...

Erik Veenstra

6/12/2007 4:10:00 PM

0

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