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
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-forum.com/\.
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-forum.com/\.