2010/7/28 Rolf Pedersen <rolfhsp / gmail.com>:
> Hi Xavier
>
> Having "include Base" in both the Interface::xxx modules does remove the
> current error message.
> But the problem is that the code in the "add_interface_methods" then runs
> with Interface::xxx modules as self, and not MyClass.
> So the methods are not collected in the @interface_methods attribute of
> MyClass but in the separate Interface::xxx modules, causing
> enforce_interface to fail, because of @interface_methods in MyClass is nil.

Indeed, that's what I meant by "I'm not sure that's what you want in
the end" :-)

> With regards to your code (I have just skimmed through it), I think you are
> limiting yourself to just one interface when you are using inheritage.
> My idea was to enforce compliance with a set of interfaces, by ensuring that
> the correct methods (and maybe later on, the correct attributes) are
> implemented.

I was trying to get something as simple as possible and only use class
inheritance, so yes: only one possible.

> I understand what you were trying to do by using a hook method to
> automatically run the "enforce" part at the end, but I guess that the
> inherited hook method will be called before the methods are defined, as you
> suspected yourself.
> I was actually thinking along these lines myself when I first wrote the
> code. But I concluded that I needed a class macro to be called after I
> defined the methods in MyClass.
> Now, I could have done all the work in this one class macro (doing both the
> implement_interface and enforce_interface parts in one call), but I also
> would like to have the definitions at the top of the class for esthetic
> reasons.

That's what we're all looking for :-)

> But then, as I said, I also thought about hook method to replace the call to
> enforce_interface, but I couldn't find one that fits...

Me neither. The only way I could think of would be to hook on allocate
or new, but, uh...this is ugly !

> Maybe Module.closing would be a good idea to have in Ruby core??? :o)
> Module.closing would be called just before closing a class, and could be
> used to do some checking.

That'd be great. Anyhow, I don't know if it would really make sense,
since Ruby classes and modules may be reopened and methods added
dynamically.

> Ok, I hope that potential new readers of this thread understand that I'm not
> advocating interface checking in Ruby, It's all just a little thought
> experiment to explore the possibilities of class macros.

I understand that it's not the Ruby way of life, due to its dynamic
component, but sometimes I miss this feature. In my case, I would like
to define a class which must be extended with mandatory methods. The
way I'm dealing with it is by providing a template .rb file with empty
mandatory methods, but that'd be great if one could just extend my
class and see by himself which methods are missing, without needing to
rerun the script a zillion times.

-- 
Xavier NOELLE