Another nil concern

irb(main):012:0> nil =~ /foo/
=> false

Should’nt it return nil instead?

BR
nasir

From: Nasir K. [mailto:[email protected]]

irb(main):012:0> nil =~ /foo/

=> false

Should’nt it return nil instead?

depends on the beholder.

:~$ qri =~
---------------------------- Multiple choices:

 Gem::Dependency#=~, Gem::Platform#=~, Object#=~,
 REXML::Light::Node#=~, RegAnd#=~, RegOr#=~, Regexp#=~, String#=~

:~$ qri object#=~
--------------------------------Object#=~
obj =~ other => false

 Pattern Match---Overridden by descendents (notably Regexp and
 String) to provide meaningful pattern-match semantics.

so you probably want,

/foo/ =~ nil
=> nil

and if failing is divine, try #match

/foo/.match nil
=> nil

nil.match /foo/
NoMethodError: undefined method `match’ for nil:NilClass

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs