[fxruby-users] Inheritance
Joel VanderWerf
vjoel at path.berkeley.edu
Thu Feb 9 14:58:31 EST 2006
Uwe Hartl wrote:
> Hi,
> I want to create an object from data I have no idea which type it is. Assuming
> I have the possibilities to have an date and an int. Now I want to create
> from both an object that has the the same capabilities (e.g. show itself in a
> parent FXFrame). But there should be specific things for the different data
> types to be modified (the textfield for the int should just allow numbers and
> the textfield of the date should allow numbers and ":" and there should be a
> button beside it which pops up a FXCalendar Dialog). what I want is basically
> to create a class (lets call it Data), which has a constructor which figures
> out from which class (DateVis or IntVis) itself should inherit. Something
> like this:
>
> class Data
> def initialize(d)
> if d.class == "Fixnum"
> inherit from IntVis
> elsif d.class == "Date"
> inherit from DateVis
> end
> end
> end
>
> I don't get it done. Does anybody have a clue if this is possible? Kind of
> inheritance after the fact of creation?
>
> Thanks
> Uwe
>
Two suggestions:
1) use modules and extend. Replace "inherit from IntVis" with
d.extend IntVis
2) use a "factory" class and some subclasses:
class Data
def self.new_from_value(d)
case value
when Fixnum; DataFixNum.new
when Date; ....
end
end
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
More information about the fxruby-users
mailing list