On Mon, 12 Nov 2007 19:31:08 -0500, Raul Parolari wrote:

> unknown wrote:
>> On Nov 12, 6:39 pm, James Edward Gray II <ja... / grayproductions.net>
>> wrote:
>>>
>>> If you don't know the contents of the Regexp and thus you call gsub
>>> (), you are a much braver programmer than I am!
>> 
>> You've never used a Regexp variable?  Always a Regexp literal for you? 
>> That's why I put a variable there, to make the point that it can't just
>> be concatenated.
>> 
>>> It's clear we just disagree and I've said my peace now.  You win.  ;)
>> 
>> In my previous post, I did however make a valid argument with a valid
>> example, to which you did not respond.
> 
> 
> The exchange was great from both sides.
> 
> But the point that JGII is making about 're' is not that he prefers
> Regexp literals vs variables! it is that if the regexp variable
> contained an anchor like \A it does not make a lot of sense to call
> gsub... because it would at most match once!
> 
> So while we may not know the exact content of the regexp (eg, it is
> provided by another component, etc), it makes sense to think that at
> least we know it is suited to a global replace (if not, what exactly are
> we doing? the whole thing loses any sense).

OK. But if re isn't suited to a global replace, then we're using sub 
instead of gsub. If we take the following examples,

filename = File.basename(input.map { |t|
   t.sub(re, "_")
}.join)

filename = input.map { |t|
   t.sub(re, "_")
}.join.as { |t|
   File.basename(t)
}

then neither of the following are equivalent

file_name = File.basename(input.join.sub(re, "_"))
file_name = File.basename(input.join.gsub(re, "_"))

--Ken

-- 
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/