Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Please tell me what this means? self.<method> in a class (http://www.velocityreviews.com/forums/t820631-please-tell-me-what-this-means-self-method-in-a-class.html)

Glenn Smith 03-29-2005 10:34 AM

Please tell me what this means? self.<method> in a class
 
Not entirely sure I understand this (it's a newbie-ruby question).

class Test
def foo
end

def Test.foo
end

def self.foo
end
end


The first definitition of foo is an instance method. The second a
class method. Perhaps my terminology is wrong but I understand what I
mean.

It's the third one I'm not sure of. The "self.foo".

What does this do, how and where would I use it?


--

All the best
Glenn
Aylesbury, UK



David Corbin 03-29-2005 11:04 AM

Re: Please tell me what this means? self.<method> in a class
 
On Tuesday 29 March 2005 05:34 am, Glenn Smith wrote:
>
> It's the third one I'm not sure of. The "self.foo".
>
> What does this do, how and where would I use it?


The self.foo method definition is redefining Test.foo. It's kind of a
shorthand notation. You'd use by saying "Test.foo". The important lesson
here, is that class/module definitions are in fact executing ruby code in the
context of the class/module, hence, there is a "self" which is the
class/module object.


David



David A. Black 03-29-2005 11:11 AM

Re: Please tell me what this means? self.<method> in a class
 
Hello --

On Tue, 29 Mar 2005, Glenn Smith wrote:

> Not entirely sure I understand this (it's a newbie-ruby question).
>
> class Test
> def foo
> end
>
> def Test.foo
> end
>
> def self.foo
> end
> end
>
>
> The first definitition of foo is an instance method. The second a
> class method. Perhaps my terminology is wrong but I understand what I
> mean.
>
> It's the third one I'm not sure of. The "self.foo".
>
> What does this do, how and where would I use it?


Every time you do this:

def some_object.some_method
...
end

you create a singleton method some_method for the object some_object
-- that is, a method that only some_object can call.

If you do the above using 'self' as the receiver, then the singleton
method you create will belong to whatever 'self' was at the time.

In your example, self is actually Test, the class whose scope you are
in. So, in effect, Test.foo and self.foo are the same, in that
context.


David

--
David A. Black
dblack@wobblini.net



Glenn Smith 03-29-2005 11:29 AM

Re: Please tell me what this means? self.<method> in a class
 
Ah. That's what confused me. I couldn't find it in pickaxe2.

Thanks David(s!)

Glenn


On Tue, 29 Mar 2005 20:11:01 +0900, David A. Black <dblack@wobblini.net> wrote:
> Hello --
>
> On Tue, 29 Mar 2005, Glenn Smith wrote:
>
> > Not entirely sure I understand this (it's a newbie-ruby question).
> >
> > class Test
> > def foo
> > end
> >
> > def Test.foo
> > end
> >
> > def self.foo
> > end
> > end
> >
> >
> > The first definitition of foo is an instance method. The second a
> > class method. Perhaps my terminology is wrong but I understand what I
> > mean.
> >
> > It's the third one I'm not sure of. The "self.foo".
> >
> > What does this do, how and where would I use it?

>
> Every time you do this:
>
> def some_object.some_method
> ...
> end
>
> you create a singleton method some_method for the object some_object
> -- that is, a method that only some_object can call.
>
> If you do the above using 'self' as the receiver, then the singleton
> method you create will belong to whatever 'self' was at the time.
>
> In your example, self is actually Test, the class whose scope you are
> in. So, in effect, Test.foo and self.foo are the same, in that
> context.
>
> David
>
> --
> David A. Black
> dblack@wobblini.net
>
>



--

All the best
Glenn
Aylesbury, UK



Robert Klemme 03-29-2005 12:10 PM

Re: Please tell me what this means? self.<method> in a class
 

Additional remarks:

- Preferably use "self" instead of the class name in order to minimize
the number of places in the code you have to touch if the class name
changes.

- You can as well use the class << notation either way

class Test
class <<self
def foo() "foo" end
end

class <<Test
def bar() "bar" end
end
end

>> Test.foo

=> "foo"
>> Test.bar

=> "bar"

Of course you can define multiple methods with this in one
class<<self...end like for "normal" classes.

Kind regards

robert


Brian Candler 03-30-2005 08:53 AM

Re: Please tell me what this means? self.<method> in a class
 
On Tue, Mar 29, 2005 at 08:29:34PM +0900, Glenn Smith wrote:
> Ah. That's what confused me. I couldn't find it in pickaxe2.
>
> Thanks David(s!)


There's more here:
http://www.rubygarden.org/ruby?SingletonTutorial



kb9agt@gmail.com 02-14-2013 02:16 AM

Re: Please tell me what this means? self.<method> in a class
 
On Tuesday, March 29, 2005 5:34:07 AM UTC-5, Glenn Smith wrote:
> Not entirely sure I understand this (it's a newbie-ruby question).
>
> class Test
> def foo
> end
>
> def Test.foo
> end
>
> def self.foo # self is an instance of the current Object.
> end
> end
>
>
> The first definitition of foo is an instance method. The second a
> class method. Perhaps my terminology is wrong but I understand what I
> mean.
>
> It's the third one I'm not sure of. The "self.foo".
>
> What does this do, how and where would I use it?

Comment out def Test.foo ... end
You're already in Test class. Who is self?
Think about it.
> --
>
> All the best
> Glenn
> Aylesbury, UK



All times are GMT. The time now is 09:15 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57