T
Thomas
Hi,
This problem maybe results from a bad understanding of what I'm trying
to do. Anyway, is it possible to call a method when creating a new class
or inheriting from another class?
Currently my code looks somehow like this:
class A
class << self
attr_reader :x
def prepare
# do something
end
def fixate
# do something
end
def foo(a)
# do something with a and @x
end
end
end
class B < A
prepare
@x = "something"
foo("bar")
def method1
# do something else
end
fixate
end
Now in an ideal world, the definition of class B would be simply:
class B < A
@x = "something"
def method1
# do something
end
end
and A.setup and A.fixate should be automagically invoked whenever
defining a descendant from class A.
Does somebody know a solution for how this can be done? The only
possible way of doing this I could come up with is to wrap the
definition part into a block and pass this block to a method that calls
these functions and evaluates the block using class_eval. Something in
the line of (untested):
class C < A
wrapper do
@x = "something"
def method1
# do something
end
end
end
But I'm not sure if this solution really is an advantage.
Cheers,
Thomas.
This problem maybe results from a bad understanding of what I'm trying
to do. Anyway, is it possible to call a method when creating a new class
or inheriting from another class?
Currently my code looks somehow like this:
class A
class << self
attr_reader :x
def prepare
# do something
end
def fixate
# do something
end
def foo(a)
# do something with a and @x
end
end
end
class B < A
prepare
@x = "something"
foo("bar")
def method1
# do something else
end
fixate
end
Now in an ideal world, the definition of class B would be simply:
class B < A
@x = "something"
def method1
# do something
end
end
and A.setup and A.fixate should be automagically invoked whenever
defining a descendant from class A.
Does somebody know a solution for how this can be done? The only
possible way of doing this I could come up with is to wrap the
definition part into a block and pass this block to a method that calls
these functions and evaluates the block using class_eval. Something in
the line of (untested):
class C < A
wrapper do
@x = "something"
def method1
# do something
end
end
end
But I'm not sure if this solution really is an advantage.
Cheers,
Thomas.