I'd like to create a Set that only allows a certain kind of (kind_of?
) objects to be added and an exception to be raised on the attempt to add an alien object.
I haven't found any resources yet and before I start messing around with the ruby core classes I'd be grateful for any advice how to achieve this in an "unobtrusive" manner.
Addition: My goal actually is to create a "templated" container class much like in C++ (e.g. Set) so that I can set the type once at definition of an instance and compare two sets on a class level if they are the same (i.e accept the same type) but keeping interoperability with "default" Sets, as an example set_instance_a.class == set_instance_b.class
should yield true if they accept the same type of object.
An idea I had was to overload the ::[]
operator so that I could write something like my_set = MySet[Foo]
which should return a MySet
instance that only accepts objects of type Foo
Thanks!
Class
is an object too so you can create new classes at runtime as needed. TheClass
constructor allows you to specify the base class and even evaluate code within the context of the new class:class_eval
lets you define methods and aliases. All this means that you can say things like this:And then:
I patched the
MySet
method intoKernel
to be consistent with theArray
,Complex
, ... methods-that-pretend-to-be-functions, you can put it anywhere you'd like.