【发布时间】:2009-11-05 10:21:29
【问题描述】:
我正在尝试创建一个 Ruby 类,它的类型非常灵活,并希望可以根据其初始化的值从许多其他类继承属性:
class Test
def initialize(type,etc)
case type
when "stringio"
inherit_from_stringio_with_data(etc)
when "list"
inherit_from_enumerable_with_data(etc)
# and so on
end
end
end
Test.new("list").each do |item|
p item
end
s = Test.new("stringio")
s.seek(3)
puts s.read(2)
我知道 - 或者更确切地说已经阅读过 - mixin 的强大功能,但据我所知,这并不是完全正确的。有没有人有任何想法,或者我是否正在尝试其他方式最好的方法(例如,@content,其中包含etc 作为StringIO、Enumerable 等)。
谢谢!
【问题讨论】:
标签: ruby class inheritance