T
Trans
A notion came to me yesterday with regards to how we extend classes.
On the one hand we can create a module and include it into a class,
but this will not allow us to override a preexisting method -- for
that we must reopen the class and play the alias and/ or binding game.
Various ideas have been put forth for simplifying and improving the
robustness of this, from #wrap_method, alias_method_chain, as well
as re, ost methods and my own cuts (albeit they go a bit further
than just method wrapping). But occurs to me that these add
complexities onto something that is already straightforward if we are
willing to separate out all class concerns into modules. In other
words instead of writing:
class X
def a; "a"; end
end
write:
module Xa
def a; "a"; end
end
class X
include Xa
end
X.new.a #=> "a"
Then it's easy enough to overlay new behaviors:
module Xb
def a; "{"+super+"}"; end
end
class X
include Xb
end
X.new.a #=> "{a}"
Since that works so well, it occurs to me, why not make this the
fundamental behavior of defining a class? In other words defining a
class:
class X
def a; "a"; end
end
could be equivalent to writing:
class X
include( Module.new{
def a; "a"; end
} )
end
So there would always be a module involved in the definition of a
class. And classes become simply containers of modules.
T.
On the one hand we can create a module and include it into a class,
but this will not allow us to override a preexisting method -- for
that we must reopen the class and play the alias and/ or binding game.
Various ideas have been put forth for simplifying and improving the
robustness of this, from #wrap_method, alias_method_chain, as well
as re, ost methods and my own cuts (albeit they go a bit further
than just method wrapping). But occurs to me that these add
complexities onto something that is already straightforward if we are
willing to separate out all class concerns into modules. In other
words instead of writing:
class X
def a; "a"; end
end
write:
module Xa
def a; "a"; end
end
class X
include Xa
end
X.new.a #=> "a"
Then it's easy enough to overlay new behaviors:
module Xb
def a; "{"+super+"}"; end
end
class X
include Xb
end
X.new.a #=> "{a}"
Since that works so well, it occurs to me, why not make this the
fundamental behavior of defining a class? In other words defining a
class:
class X
def a; "a"; end
end
could be equivalent to writing:
class X
include( Module.new{
def a; "a"; end
} )
end
So there would always be a module involved in the definition of a
class. And classes become simply containers of modules.
T.