From: Dave Oshel Date: 2003-05-24T04:06:56+09:00 Subject: Re: why is "does" missing from this sub!-stitution? In article <20030523080548.GA4196@uk.tiscali.com>, Brian Candler wrote: > On Fri, May 23, 2003 at 08:44:16AM +0900, Dave Oshel wrote: > > Maybe so, but this works as expected: > > > > `/sw/bin/fortune -m Nillity`.each { |x| > > x.sub!( /(_+\010+)([a-zA-Z]+\b)/, "" + '\2' + "" ) > > print x > > } > > > > Maybe it's a bug ... or a feature ... or a bug ... or a bug AND a > > feature ... ;-) > > It's definitely a feature and not a bug. $2 behaves like a regular variable, > and as you've seen, you have to use normal string interpolation #{...} to > insert it. If $2=="foo" and you write the string "#{$2}" then it is > interpolated as "foo", and *then* the resulting string is what you > are passing as a parameter to 'sub'. If $2 is nil, then you pass "". > So sub has no idea where to substitute any value. > > However the string '\2' is treated as a special case by sub itself. When it > sees that sequence, instead of inserting \ and 2 into the result, it inserts > the value of $2. > > It would be very strange if there were strings with some sort of 'deferred > interpolation', i.e. the string contained the sequence '#{expr}' but expr is > re-evaluated every time you read the string. Ruby is flexible enough that > you could implement that if you wanted though! > > Regards, > > Brian. > There's too much computer science going on here. I don't actually care WHY case 2 doesn't work, as long as I know case 1 DOES work. Thanks for taking the trouble to suggest causes, though. It does help. # case 1 -- the way that worked, after several hours of headscratching `/sw/bin/fortune -m Nillity`.each { |x| x.sub!( /(_+\010+)([a-zA-Z]+\b)/, "" + '\2' + "" ) print x } =begin (science) % Everyone knows that dragons don't exist. But while this simplistic formulation may satisfy the layman, it does not suffice for the scientific mind. The School of Higher Neantical Nillity is in fact wholly unconcerned with what does exist. Indeed, the banality of existence has been so amply demonstrated, there is no need for us to discuss it any further here. The brilliant Cerebron, attacking the problem analytically, discovered three distinct kinds of dragon: the mythical, the chimerical, and the purely hypothetical. They were all, one might say, nonexistent, but each nonexisted in an entirely different way ... -- Stanislaw Lem, "Cyberiad" % =end # case 2 -- the way I assumed it would it work `/sw/bin/fortune -m Nillity`.each { |x| x.sub!( /(_+\010+)([a-zA-Z]+\b)/, "#{$2}" ) print x } =begin (science) % Everyone knows that dragons don't exist. But while this simplistic formulation may satisfy the layman, it does not suffice for the scientific mind. The School of Higher Neantical Nillity is in fact wholly unconcerned with what exist. Indeed, the banality of existence has been so amply demonstrated, there is no need for us to discuss it any further here. The brilliant Cerebron, attacking the problem analytically, discovered three distinct kinds of dragon: the mythical, the chimerical, and the purely hypothetical. They were all, one might say, nonexistent, but each nonexisted in an entirely different way ... -- Stanislaw Lem, "Cyberiad" % =end -- ["10110100101101000101000000100010100001100110111010010110001001100000010011000010011101000000010011110010110011100001011010100110001101101001000010010000001001101100011011110110110011100001011010100110001101100000001010110110100001101100011001110100110001101111011010110110010100001100001010100110001001101000011001001110000001000100101010000110000011101001011000100110110011100011010000000100100100101000001010010000000101100010111000101110000011100101110011110100111101000001011011110110101101101010011000001110100001101110011010100110011101001011011010000110110001100111010011000110111101101011011011110100001001101100011011110110110011100001011010100110001101101111010001010000010001001001101010100110001011100000010010000010011101101111011000101110000101101010011001001110000001000100101010101110010001101001111000000100000100101000011011000110110101101010011001001110"].pack("b*")