> what is the use of mixin and what is the use of inheritance
"Programming Ruby (2nd)" :
p 119:
In fact, mixed-in modules effectively behave as superclasses."
p. 355:
If a module is included in a class definition, the module's constants,
class variables, and instance methods are effectively bundled into an
anonymous(and inaccessible) superclass for that class....Calls to
methods not defined in the class will be passed to the module(s) mixed
into the class before being passed to any parent class.
module Greet
def friendly_greet
puts "Hello."
end
def angry_greet
puts "Go away."
end
end
class NicePerson
include Greet
end
class MadPerson
include Greet
end
class SadPerson
include Greet
def friendly_greet
puts "Cry, hi, cry."
end
end
np = NicePerson.new
np.friendly_greet #Hello.
puts
mp = MadPerson.new
np.angry_greet #Go away.
puts
sp = SadPerson.new
sp.friendly_greet #Cry, hi, cry.
sp.angry_greet #Go away.
--
Posted via http://www.ruby-forum.com/.