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

G

Glenn Smith

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

David Corbin

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
 
D

David A. Black

Hello --

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
 
G

Glenn Smith

Ah. That's what confused me. I couldn't find it in pickaxe2.

Thanks David(s!)

Glenn
 
R

Robert Klemme

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
=> "bar"

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

Kind regards

robert
 
K

kb9agt

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.
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top