I'm trying to use methods from that is extended by the class that this class is extending from. An example of what I'm trying to do:
class A
def foo
"Foobar"
end
end
class B
extend A
end
class C
extend B
end
B.foo #=> "Foobar"
C.foo #=> "Foobar"
I'm not sure if this type of functionality is available in Ruby. I know that this is possible by changing extend
to include
in B
, but I'd like the methods available in A
as class methods in B
as well as C
.
extend
and include
are for modules; you cannot, to my knowledge, use modules with extend
and include
(in fact Ruby will raise an error). Instead you should define A as a module and then extend
B and C with A. See John Nunemaker's RailsTips write-up on extend
and include
to get a better handle on this design pattern.
Another option to do this is to have B and C inherit from A, like so:
class A
def self.foo
"Foobar"
end
end
class B < A; end
class C < B; end