From: Robert Klemme Date: 2007-10-11T15:45:12+09:00 Subject: Re: will #in? be in ruby2? (was Re: Finding shared elements between to arrays.) 2007/10/10, mortee : > David A. Black wrote: > > Hi -- > > > > On Wed, 10 Oct 2007, mortee wrote: > >> Why don't you just define #in? in Object yourself, and then you can use > >> it anywhere in your code. That's something I really like about Ruby: if > >> I'm missing something from a core or library class, I can just drop it > >> in for my own use. > > > > It's not risk-free, though, since if two people define a given method > > differently, one of them will get trampled on. > > Yeah, that's what the other thread "Open-Closed..." is about in part: > method name collisions whithin a module/class coming from different sources. > > I think that this problem should be solved somehow instead of avoiding > the power of ability to extend arbitrary classes. I'm absolutely aware > of the issue though. While reading this thread another possible solution came to my mind: what about an operator like "def_once" or "def!" which will raise an exception if the method is defined for that class already? Of course, we could cook our own using define_method but there are some restrictions so it's not exactly equivalent to def. Semantics would have to be carefully chosen. At the moment it seems that "def_once" should raise only if a) the instance's class has an instance method of the same name already or b) there is a singleton method of the same name. However, I am not sure how to deal with the situation when there is an instance method in the class and then a singleton method definition is attempted. What do you guys think? This would at least make sure that you immediately notice if a method is defined twice and it seems to easier integrate with the language than selector namespaces. Kind regards robert