Simulating Multiple Inheritance with Mixins.rb : Mixins « Language Basics « Ruby






Simulating Multiple Inheritance with Mixins.rb


require 'set' 

module Taggable
  attr_accessor :tags

  def taggable_setup
    @tags = Set.new
  end

  def add_tag(tag)
    @tags << tag
  end

  def remove_tag(tag)
    @tags.delete(tag)
  end
end

module Taggable2
  def initialize(a,b,c)
  end
end

class TaggableString < String
  include Taggable
  def initialize(*args)
    super
    taggable_setup
  end
end

s = TaggableString.new('this is a test.')
s.add_tag 'A'
s.add_tag 'B'
s.tags

 








Related examples in the same category

1.Creating Mixins
2.The Making of a Man
3.Automatically Initializing Mixed-In Modules
4.Mixing in Class Methods.rb