Hello all,
I’m getting an erorr that I don’t understand.
My code
while gets.
break if (/<!-{19} ending left_colum -{11}>/)
end
Output:
warning: regex literal in condition
What is a “regex literal”? How is that different from any other regex?
I just want to read a text file and skip all the lines until I arrive at
the one matching the expression above.
Can anyone see what I’m doing wrong? I really can’t.
Thanks.
Daniel Carrera | OpenPGP fingerprint:
Mathematics Dept. | 6643 8C8B 3522 66CB D16C D779 2FDD 7DAC 9AF7 7A88
UMD, College Park | http://www.math.umd.edu/~dcarrera/pgp.html
Daniel,
I’m getting an erorr that I don’t understand.
My code
while gets.
break if (/<!-{19} ending left_colum -{11}>/)
end
You've probably already figured this out, but the problem is the dot (.)
after the call to gets in the first line. I’m not too sure at first glance
how ruby is parsing this, but any way you look at it, it’s gonna’ get it
wrong.
I hope this helps!
- Warren Brown
Daniel Carrera wrote:
My code
while gets.
break if (/<!-{19} ending left_colum -{11}>/)
end
Output:
warning: regex literal in condition
What is a “regex literal”? How is that different from any other regex?
I just want to read a text file and skip all the lines until I arrive at
the one matching the expression above.
A literal is something written directly into the source.
“hi” = String literal
/hi/ = Regexp literal
Regexp.new( “hi” ) = String literal passed to Regexp constructor
Ruby does some freaky stuff in some cases, such as when you have a
literal in an if, the exact usage you are looking for: an implicit
match. Notice how if you do
re = /<!-{19} ending left_colum -{11}>/
while gets
break if re
end
then “gets” is only called once because any object instance turns true
in a boolean condition. My money is on Matz wanting to deprecate the
implicit matching in the future and this is your very early and literal
warning.
HTH
···
–
([ Kent Dahl ]/)_ ~ [ http://www.pvv.org/~kentda/ ]/~
))_student_/(( _d L b_/ (pre-) Master of Science in Technology )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)
Thanks for the explanation, but I’m not quite following.
I now understand what a string literal is, but I don’t quite get the
problem at the if.
HTH wrote:
[snip]
Notice how if you do
re = /<!-{19} ending left_colum -{11}>/
while gets
break if re
end
then “gets” is only called once because any object instance turns true
in a boolean condition.
Uhm… Let’s see if I understand what you are saying. “re” here is an
instance of the class Regexp. Since it’s an object instance, it is
interpreted as “true” by the if. Did I understand that right?
What I don’t follow is:
- Why would that be the case?
- How is a non-literal any less of an object instance than a literal.
I guess that the best solution is to use ($_ =~ re). But I’d still like
to understand why Ruby behaves this way.
Here is another example of weird example:
$ irb
re = /hi/
$_ = “hello”
puts “yes” if ($_ =~ re)
=> nil
puts “yes” if re
yes
=> nil
Thanks for the help.
···
–
Daniel Carrera | OpenPGP fingerprint:
Mathematics Dept. | 6643 8C8B 3522 66CB D16C D779 2FDD 7DAC 9AF7 7A88
UMD, College Park | http://www.math.umd.edu/~dcarrera/pgp.html
Moin!
Daniel Carrera wrote:
Thanks for the explanation, but I’m not quite following.
I now understand what a string literal is, but I don’t quite get the
problem at the if.
Well, in Ruby constructs like ‘if /foo/’ currently have a special
meaning. It’s not just a check to see whether ‘/foo/’ is true as it is
in all other cases (like, for example, ‘if 5’).
However in Perl ‘if /foo/’ really means ‘if $_ =~ /foo/’ and I guess
that Ruby is just imitating that behaviour so that Perl users aren’t
suprised.
I think that matz wants to deprecate this as it is a special case (and
special cases are Bad Things) – you could get the current behaviour
without a warning with ‘if ~/foo/’:
---------------------------------------------------------- Regexp#~
~ rxp → anInteger or nil
Match—Matches rxp against the contents of $. Equivalent to rxp
=~ $.
$_ = “input data”
~ /at/ #=> 7
To make long things short: ‘if /foo/’ should be replaced with ‘if
~/foo/’ when wanting to match.
Thanks for the help.
No problem! 
Regards,
flgr