From: Florian Gross Date: 2004-12-09T09:32:31+09:00 Subject: Re: [ANN] Ruby/Extensions v0.6.0 Gavin Sinclair wrote: > On Thursday, December 9, 2004, 6:57:28 AM, Florian wrote: >>Thanks, I was fearing that there would be name clashes with the >>Ruby-Junction library which defines Enumerable#none, #any, #all, >>#with_count and #exactly. > You might be interested to know that 'extensions' is careful about > adding methods. If, for example, you did this: > > class Array; def rand; ...; end; end > > require 'extensions/array' > > then you'd get a warning on STDERR, something like > > WARNING: Array#rand exists, not overwriting Oh, that's quite a nifty feature. Well done! >>Array#rand sounds interesting, but I wonder if it does not lead to >>trouble when using rand() from a custom overloaded Array or Enumerable >>method. > What might an example of this trouble be? For a start, it's only > defined on Array, not Enumerable. And its definition is very simple: > > def rand > idx = Kernel.rand(self.size) > at(idx) > end > > So any array that properly implements size and at should do the trick. I'm thinking about something like this: (Made-up sample, does not make much sense in isolation -- but it could also happen in real code.) class Enumerable def find_some(chance = 0.5) find_all { rand < chance } end end I think using the Array#rand extension would lead to trouble in this case: [1, 2, 3].find_some Because Enumerable#find_some expects to call Kernel#rand. I'm not sure if it is necessary to rename it (Array#pick_rand seems to be a common candidate), but a note in the documentation might be a good thing. > BTW Florian, while I've got you on this subject, I must apologise for > an incorrect attribution in extensions: Florian Frank appears in there > on one occasion instead of you. FINR :) Oh, no problem there -- we tend to get confused a lot. I guess one could refer to me as 'flgr' instead in case it would help.