From: Joel VanderWerf Date: 2008-02-29T02:59:40+09:00 Subject: Re: Proposed Solutions - Was [ Re: Monkeypatching is Destroying Ruby ] Trans wrote: > On Feb 28, 9:59 am, Sam Smoot wrote: > >> Now if we ever did get selector-namespaces, then I'd be completely >> against this. But since it appears we're _not_ getting the feature, >> this isn't a half-bad substitute IMO. > > Well, while I'm sitting here thinking about it, let's try another > concept for selector namespaces: > > class Array > def to_csv for FasterCSV > # ... > end > end > > class String > def to_a for FasterCSV > end > > "".to_a # will use Ruby's > [].to_csv # NoMehtodError > > space FasterCSV > > "".to_a # will use FasterCSV's > > Anything defined within FasterCVS will use the FasterCSV space by > default, in which case you'd have to say "space Ruby" to get access to > pure. > > Space selections are reset per file. Could even do this now, using semi-obscure method names instead of namespaces: class Array def to_csv_for_FasterCSV # ... end end class FasterCSV def from obj # class name of obj isn't embedded in method name! obj.to_csv_for_FasterCSV end end a = [1,2,3] csv = FasterCSV.from(a) We don't have to switch on the type of a to call the right FasterCSV method (or switch on the type of a to define a singleton #to_csv method). The chance of collision is greatly reduced by explicit naming. The main disadvantage compared with Trans's suggestion is that within FasterCSV you have to use longer method names. But the "public" API is kept clean because you use FasterCSV.from rather than #to_csv_for_xxx. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407