Techniques to overcome the single inheritance issue?
what techniques are there in ruby to overcome the single inheritance rule?
开发者_如何学运维is it just using modules?
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
精彩评论