[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regexp ops: get rid of !=~?

Gary Yngve

2/10/2009 5:29:00 PM

[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

2 Answers

Brian Candler

2/11/2009 2:58:00 PM

0

Gary Yngve wrote:
> "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.
--
Posted via http://www.ruby-....

Brian Candler

2/11/2009 3:01:00 PM

0

Gary Yngve wrote:
> 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
--
Posted via http://www.ruby-....