"regex literal in condition"

D

Daniel Carrera

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (SunOS)

iD8DBQE/HwDgnxE8DWHf+OcRAk2WAKDutetKGyuxPb13sA6l5cGE95Us/wCfUAAa
ZaoQRjmY5pDZ6RGFegmh2YQ=
=WHdb
-----END PGP SIGNATURE-----
 
K

Kent Dahl

Daniel said:
# 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
 
D

Daniel Carrera

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:

$ irbyes
=> nil

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top