"Self" confusion

L

Leslie Viljoen

Can someone explain why the following produces different results? In
another question I wrote that by default, programs run inside the
definition-space of Object, but now I think that I was wrong:

class Yoghurt
def self.a
"yoghurt self"
end
def Yoghurt.a
"yoghurt"
end

p Yoghurt.a
p self.a
end


def self.a
"object self"
end
def Object.a
"object"
end

p Object.a
p self.a

------------------------Output:
"yoghurt self"
"yoghurt self"
"object"
"object self"
 
J

Justin Collins

Leslie said:
Can someone explain why the following produces different results? In
another question I wrote that by default, programs run inside the
definition-space of Object, but now I think that I was wrong:

class Yoghurt
def self.a
"yoghurt self"
end
def Yoghurt.a
"yoghurt"
end

p Yoghurt.a
p self.a
end


def self.a
"object self"
end
def Object.a
"object"
end

p Object.a
p self.a

------------------------Output:
"yoghurt self"
"yoghurt self"
"object"
"object self"
Really? Because I get:

"yoghurt"
"yoghurt"
"object"
"object self"

:)

I thought the top-level stuff rain inside an instance of Object?

Try:

p Object.class #=> Class, naturally
p self.class #=> Object

class A
p self.class #=> Class
end

a = A.new

class<<a
p self.class #=> Class
end

So, if we were really in the definition space of Object, then self.class
would be class, not Object.


-Justin
 
J

Justin Collins

Justin said:
Really? Because I get:

"yoghurt"
"yoghurt"
"object"
"object self"

:)

I thought the top-level stuff rain inside an instance of Object?

Try:

p Object.class #=> Class, naturally
p self.class #=> Object

class A
p self.class #=> Class
end

a = A.new

class<<a
p self.class #=> Class
end

So, if we were really in the definition space of Object, then
self.class would be class, not Object.


-Justin

I meant to add some "I think"s and "I guess"s in there somewhere.

-Justin
 
S

Schüle Daniel

class Yoghurt
I am not Ruby expert, but isn't Yogurt.a and self.a the same thing?
Then the last one simply overwrites the preceding one.

From this I think that
Object.a and self.a should act similary, don't they?
 
L

Leslie Viljoen

Now I am fully confused! How do you define methods inside an instance
of an object?
Using <ObjectInstance>.class_eval? Is that what IRB does at the top level?

Les
 
P

Phillip Hutchings

See comments in the code.

class Yoghurt
def self.a # Here self is the Yoghurt class. This defines a method
on the Yoghurt class
"yoghurt self"
end
def Yoghurt.a # This does exactly the same as self.a, just another
way of doing it.
# This overwrites the previous version, which is quite legit in Ruby
"yoghurt"
end

p Yoghurt.a
p self.a
end


def self.a
# This defines a on self, which is an instance of Object.
"object self"
end
def Object.a
# This defines a on the Object class
"object"
end
p Object.a
p self.

And the output I'd expect is:
 
P

Phillip Hutchings

Just like that ;) Pick up a copy of Programming Ruby, it's got a
better explanation than I could come up with.

Basically when you do:
x = Object.new
def x.foo
'bar'
end

you're added a singleton class on to x that contains the instance
method foo. Only x can use this, no other Object instance will have
it.

So likewise when you're in an instance of Object, as ruby scripts
start out in, self.foo is creating an instance method in the singleton
class of the Object instance.
 
L

Leslie Viljoen

Thanks everybody, this really helps!
So top level is an Object singleton referenced through 'self'.

I think I get it.

Part of my problem was that 'self' refers to the enclosing class when
using def self.x, but within the new function, self refers to an
object. Very interesting.

Les
 
A

Aleks Kissinger

Well, in both cases, it refers to an object. When you are in the
top-level of a class definition, "self" actually refers to the class
object. That is to say, the instance of Class your new class
definition creates.

class Foo
puts self
puts self.class
end

=> Foo
=> Class

So using self adds a method to the object "self", which happens to be
an instance of class. ie. class methods.
 
A

Aleks Kissinger

Oh, sorry, I slightly mis-read. Yes, you probably know most of that.
Anyway, "new" is an instance method of the new object created, so I
see what you are saying.

.....oops
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top