Regexp ops: get rid of !=~?

G

Gary Yngve

[Note: parts of this message were removed to make it a legal post.]

The proper way to see if something doesn't match is
"foo" !~ /bar/

The ~/blah/ operator exists to apply to $_

Unfortunately this means that

"foo" !=~ /bar/

does

"foo".!= (~bar), which doesn't produce an error, even though it's basically
garbage.
Instead it fails silently by returning true (nil or an number are not
strings).

Is there a way w/ Ruby to define a mega !=~ operator and either have it do
!~ or have it throw an exception?
Or an easy way w/ Ruby to have the parser warn about it?

Alas google is not good for searching for !=~, so apologies if this has been
previously discussed.

Thanks,
Gary
 
B

Brian Candler

Gary said:
"foo" !=~ /bar/

does

"foo".!= (~bar)

Actually: "foo" != ~/bar/
which doesn't produce an error, even though it's
basically
garbage.
Instead it fails silently by returning true (nil or an number are not
strings).

The problem here is ~/bar/

This is an ugly Perlism in Ruby which hardly anybody ever uses anyway.
Search for Regexp#~ in ri.

Personally, I would be very happy to lose this entirely, together with
all the other implicit tests and side-effects on $_ (such as is done by
Kernel#gets and IO#gets). Or at least have a flag to turn on this
behaviour.

Regards,

Brian.
 
B

Brian Candler

Gary said:
Or an easy way w/ Ruby to have the parser warn about it?

irb(main):001:0> Regexp.class_eval{undef_method :~}
=> Regexp
irb(main):002:0> ~/foo/
NoMethodError: undefined method `~' for /foo/:Regexp
from (irb):2
 

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

Members online

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,514
Latest member
AdeleGelle

Latest Threads

Top