A. S. Bradbury
9/13/2006 8:19:00 PM
On Wednesday 13 September 2006 21:03, Rick DeNatale wrote:
> On 9/12/06, A. S. Bradbury <asbradbury@tekcentral.org> wrote:
> > On Tuesday 12 September 2006 19:50, Rick DeNatale wrote:
> > > Nice, but it can fall down if the pattern contains a regexp which
> > > matches more than one element:
> > >
> > > a = "aaaabbabbab".split(//)
> > > a.find_pattern /aab/, 'b' #=> nil
> > >
> > > There's probably a way to fix that, but I'm not sure I can see how.
> >
> > I'd actually argue that's expected behaviour and you're just using it
> > wrong(!). /aab/ simply doesn't match any element, so it *should* fail.
>
> Well, I'd expect that if you allow regexps that you should allow
> regexps! But see below.
But why would it be expected behaviour for a regexp to try to match across
multiple items? The regexp is applied to a single item of the Enumerable
object, surely that's the only sensible way to deal with this?.
%w[This is an example].find_pattern /an example/ #=> false.
Surely you wouldn't expect the above snippet to automagically match?
> > The Enumerable mixin provides #to_a, but relying on this seems like a
> > poor solution...
>
> Not sure why.
I'm just thinking that then you have to build the array in memory. It's never
going to be a big deal unless you have *really large* arrays. It just seems
cleaner to seek a solution that examines a single member of the Enumerable
object at a time.
> > The string approach really doesn't seem like the right way to go about
> > this, if I understand you correctly. Not for the problem I'm trying to
> > solve at least. The idea of #find_pattern is that it will work for any
> > arbitrary object.
>
> But then regexps won't work except for a string.
Well no, #find_pattern isn't meant to be tied to regexp matching. You just
could use it for that. I'm not sure I'm really following you that well on
this point.
> Also, strings are a funny kind of enumerable here, since by default,
> they just yield themselves in each, unless they include newlines. I'd
> think that the general use of find_pattern in a string would be to
> search for the pattern in the string, not as a pattern of the lines in
> the string.
Well, you'd use it in a way that is meaningful for your purposes. I'm
basically using this to find patterns in an array of tokens. If my
tokenization rules were simple, perhaps a regexp could be compiled, but it's
really not possible in this situation.
Alex