From: Logan Capaldo Date: 2007-08-14T19:18:34+09:00 Subject: Re: Split a string based on change of character ------=_Part_34957_30782180.1187086714619 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 8/14/07, Brad Phelan wrote: > > Brad Phelan wrote: > > Pe=F1a wrote: > >> From: Logan Capaldo [mailto:logancapaldo@gmail.com] # On 8/13/07, Brad > >> Phelan wrote: > >> # > Enumerator.new(s, :scan, /(.)\1*/).map {$&} > >> # What's wrong with s.enum_for(:scan, /(.)\1*/).map { $& } ? > >> > >> somehow i missed the enumerator hack. thank you logan and brad for the > >> update. > >> kind regards -botp > >> > > > > Hey cool .. 'enum_for' exactly what I was looking for. I couldn't > > understand why it didn't exist and it does. Scratch my suggestion > > for 'enum_scan'. > > > > B > > Would it not be clearer if enum_for worked as > > s.enum_for.scan(/(.)\1*/).map { $& } > > Quickie > > > require 'enumerator' > > module Enumerable > class Expr > def initialize( enum ) > @enum =3D enum > end > > def method_missing(name, *args) > @enum.enum_for_old(name, *args) > end > end > > end > > class Object > alias :enum_for_old :enum_for > def enum_for > Enumerable::Expr.new(self) > end > end > > > s =3D "aabbccvvvvfg dddd" > r =3D s.enum_for.scan(/(.)\1*/).map {$&} > puts r > > > (To be correct though a full implementation should use the BlankSlate > class and properly implement the original enum_for interface ) That's the anti-clear IMO. Any time you use method_missing theres potential for breakage, and I don't even see a good reason in this case. s.enum[erator]_for[the method](:scan ... Any time I'm tempted to use . as an argument separator I think about variables, ie foo =3D s.enum_for ... code ... foo.scan(/(.)\1*/).map { $& } Raise your hand if you think foo is a string now. ... months later ... puts "Hello, #{foo}!" B > > ------=_Part_34957_30782180.1187086714619--