is it possible in Ruby to make a method of a class calling another
method to always call the version of that method from the descendant?
I do not know how to put this more clear, so here is an example:
class A
def f
g # what to put here?
end
def g
"Haha"
end
end
class B < A
def g
"Hoho"
end
end
B.new.f # i want this to return "Hoho", but it will return "Haha"
On Thu, Aug 25, 2011 at 12:48 PM, Alexey Muranov < muranov@math.univ-toulouse.fr> wrote:
Hello,
is it possible in Ruby to make a method of a class calling another
method to always call the version of that method from the descendant?
I do not know how to put this more clear, so here is an example:
class A
def f
g # what to put here?
end
def g
"Haha"
end
end
class B < A
def g
"Hoho"
end
end
B.new.f # i want this to return "Hoho", but it will return "Haha"
In fact, i've got a problem in Rails with an abstract class
class AbstractSmartModel < ActiveRecord::Base
self.abstract_class = true
...
end
I've got an error "Could not find table 'abstract_smarter_models' ".
I thought that one of descendants called a method of AbstractSmartModel
which called another method that only a non-abstract class could have,
but apparently i was wrong.
I'll try to debug this, sorry for bad question.
I think i've found an explanation: in fact i used a lambda and called a
class method in the body of the lambda, so probably `self` was bound to
the current self (the abstract class).
Sorry again for the bad question.
@7stud thanks for a good example.
(The is a typo: the output is 1015.)
I was confused whether `self` was a method or a keyword, or a local
variable.
So, apparently it behaves like a local variable.
Also the difference between bounding to a variable and (not) to the
variable's value is tricky.
And i see that there is an essential difference between lambdas and
methods defined with `def`, as lambdas are bound to the current scope,
and methods are not.
This makes sense as lambdas are objects, and methods are called on
objects.
ruby moves up the inheritance hierarchy until it finds f():
def f
g()
end
f() executes and inside f() ruby finds the statement:
g()
ruby must call all methods with an object, and if you don't specify the
object that is calling the method, then ruby uses self. So g() is
equivalent to:
self.g()
What is self? self is the object that called the method. What object
called the method? An instance of B called f(), so inside f() self='the
B instance'. That means self.g is equivalent to:
'the b instance'.g()
So ruby checks the B class for a method called g().
In fact, i've got a problem in Rails with an abstract class
class AbstractSmartModel < ActiveRecord::Base
self.abstract_class = true
...
end
Inside a class, but outside any method definitions, self equals the
class. So the line:
self.abstract_class = true
is equivalent to:
AbstractSmartModel.abstract_class = true
I've got an error "Could not find table 'abstract_smarter_models' ".
I don't know what that indicates.
···
I thought that one of the descendants called a method of
AbstractSmartModel
which called another method that only a non-abstract class could have,
but apparently i was wrong.
I'll try to debug this, sorry for bad question.
UPDATE: the error is raised by a call to
columns_hash inside of a class method of the abstract class.
Do not understand.
I think i've found an explanation: in fact i used a lambda and called a
class method in the body of the lambda, so probably `self` was bound to
the current self (the abstract class).
Sorry again for the bad question.