What I don’t understand is that if after that I do:
f = Example.new
f.two
I still get:
two inside one
Since the two method in question is defined within one, doesn’t it
behave like a method on the object e? How can it override the two
method outside for the f object?
When the compiler first encountered Example, it plugged one() and the
outer
two() into Example’s class instance list.
The first call to one() then bonds the inner two() to the class. The
object did
not get affected in either case. (Always remember classes are objects
around here!)
If you ran the program again (a new Ruby “VM”), and never called one(),
you
would only get the outer two().
What happens when you call the one method is it redefines the two
INSTANCE METHOD at the class level (ie the context of instance method
definition in the class), which means ALL objects are affected.
I’m doing this as a learning experiment. I would have thought that
since self inside method one is an object, then two inside one would
be defined on the object, not on the class.
Phlip’s explanation made it clear to me though. It’s as if I opened
the class and redefined two, correct?
Phlip’s explanation made it clear to me though. It’s as if I opened
the class and redefined two, correct?
Yes - always think of the interpreter like a text caret skipping thru
the
program, statement by statement, from top to bottom. It interprets
‘class’ and
‘def’, but it only parses what’s inside the def, and stores it. The
interpreter
can’t even see the inner ‘too()’ (except as lexically correct tokens).
Only when
you call ‘one()’ does the interpreter go back inside and this time
actually
execute its lines.
The method is on the class, as an istnce method. There is only one
class, and all instances look to it for their methods. If you want you
can do methods on particular instances only. You probably want this
behaviour and I think it’s achieved with instance_eval. I’ll post an
example in a sec
Generally, compilation is when you take all of your resources and
build a product (in every sense of the definition). Interpretation is
when moment by moment, you translate bits as they come in.
Don’t confuse the beginners!!!
Yeah, like saying “translator” instead of “interpreter” when talking
natural (human) languages.
Also, there is nothing gained with def inside def. It is equivalent to
adding a method to the singleton class, but without the ability to
reference variables from the enclosing binding.
class A
def f
x = 3
# note: Object#singleton_class makes this cleaner
(class << self ; self ; end).instance_eval {
define_method(:g) {
puts x
}
}
end
end
a = A.new
a.f
a.g #=> 3
class B
def f
x = 9
def g
puts x
end
end
end
b = B.new
b.f
b.g #=> undefined local variable or method `x’
I’m doing this as a learning experiment. I would have thought that
since self inside method one is an object, then two inside one would
be defined on the object, not on the class.
Phlip’s explanation made it clear to me though. It’s as if I opened
the class and redefined two, correct?
Yes.
def … end is not a definition. It’s an expression. It is executed,
and it has side effects.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.