Possible Duplicate:
ruby inheritance vs mixins
The following is a frequent pattern in Ruby:
module Persistable
def initialize
# Initialize a persistable document.
end
end
class Foo
include Persistable
end
What's the advantage here over simply subclassing?
class PersistableDocument
def initialize
# Initialize a persistable document.
end
end
class Foo < PersistableDocument
end
In other words, why would you mix in behaviour that defines how an object should be instantiated if all that's accomplished is one extra line of code?