From: Charles Steinman Date: 2005-07-29T04:06:01+09:00 Subject: Re: Method Scopes Trans wrote: > I would like to see Ruby gain a "scoping feature" (I usually refer to > this as a "namespace", but looking at Matz' discussion of Rite he's > using that to mean something else). This idea is this: > > class String > > scope format > > def jumble > j = '' > split(//).each_with_index{ |c,i| j << ( i % 2 == 0 ? c.downcase > : c.upcase ) } > j > end > > end > > end > > "Wizard".format.jumble #=> wIzArD > > > Currently I can implement like so: > > require 'delegate' > > class String > > def format > @__format__ ||= Format.new( self ) > end > > class Format < DelegateClass( self ) > > def jumble > j = '' > split(//).each_with_index{ |c,i| j << ( i % 2 == 0 ? c.downcase > : c.upcase ) } > j > end > > end > > end > > But that has a whole lot of icky overhead. I would be nice if Ruby > could support this kind of thing natively. It's a nice way to organize > methods. I wouldn't be hard to create a Class#scope method that could do syntax very much like your first example. Just have it create a delegate class and instance_eval a passed block so it's executed in the scope of that class.