Blankman
Blankman

Reputation: 267190

Techniques to overcome the single inheritance issue?

what techniques are there in ruby to overcome the single inheritance rule?

is it just using modules?

Upvotes: 0

Views: 156

Answers (1)

Phrogz
Phrogz

Reputation: 303411

Yes, modules are the answer in general. If you have a more specific needs (such as having a class that merges functionality from two different classes) other options exist, such as delegation.

Note that if you need a module to provide 'class' methods to those that inherit from it, you will probably want to use this common pattern:

module Foo
  def aaa
    "hi"
  end
  module ClassMethods
    def bbb
      "mom"
    end
  end
  def self.included( other )
    other.extend ClassMethods
  end
end

class Bar
  include Foo
end

puts Bar.new.aaa, Bar.bbb
#=> hi
#=> mom

Upvotes: 2

Related Questions