Thanks. Your explanation was very helpful. It works better now, but the
range operator *still* catches more than it should. For example:
#!/usr/bin/ruby -w
## one
## two
## three
## four
## five
File.open($0) do |file|
while file.gets
print $_.sub(/^## /, '') if
($_ =~ /^## two/)...($_ =~ /^## four/)
end
end
prints up to "four" when (as I understand it) it should stop at "three"
since "## four" would make the if-statement false. Am I still missing
something obvious?
···
On Mon, Jun 04, 2007 at 04:35:25PM +0900, come wrote:
Implicit match against "$_" is not anymore supported in version 1.8.
--
"Oh, look: rocks!"
-- Doctor Who, "Destiny of the Daleks"
prints up to "four" when (as I understand it) it should stop at "three"
since "## four" would make the if-statement false. Am I still missing
something obvious?
Yes. That will never work, as this is not Perl nor are you creating a
range of regexp (which cannot be done, btw).
See what happens when the match does happen as you want:
a = 'four'
(a =~ /two/)...(a =~ /four/)
ArgumentError: bad value for range
[result is: (nil...'four') ]
Plus the uncommented # in the regex are probably making ruby assume a
comment.
This is another way of writing what you want to do:
#!/usr/bin/ruby -w
## one
## two
## three
## four
## five
File.open($0) do |file|
range = [ '$_ =~ /^## two/', '$_ !~ /^## four/' ]
while file.gets
if eval("#{range[0]}")
print $_.sub(/^## /, '');
range.shift
end
end
end
···
On Jun 4, 9:51 pm, "Todd A. Jacobs" <tjacobs- sndr-019...@codegnome.org> wrote:
On Mon, Jun 04, 2007 at 04:35:25PM +0900, come wrote:
Yes, because (as with Perl) there are two forms of conditional range:
".." and "...".
The three dots form evaluate the condition after the code. Therefore,
the "four" is printed, then the condition is evaluated to false.
The two dots form evaluates the condition before the code. So it
should works as you expect : "($_ =~ /^## two/)..($_ =~ /^## four/)".
On 5 juin, 02:51, "Todd A. Jacobs"
···
range operator *still* catches more than it should. For example:
#!/usr/bin/ruby -w
## one
## two
## three
## four
## five
File.open($0) do |file|
while file.gets
print $_.sub(/^## /, '') if
($_ =~ /^## two/)...($_ =~ /^## four/)
end
end
prints up to "four" when (as I understand it) it should stop at "three"
since "## four" would make the if-statement false. Am I still missing
something obvious?
--
"Oh, look: rocks!"
-- Doctor Who, "Destiny of the Daleks"