Timeline for How to check whether a string contains a substring in Ruby
Current License: CC BY-SA 4.0
10 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Mar 30, 2022 at 16:03 | comment | added | David Aldridge |
Alternatively, avoid sending methods to nil with a safe navigation operator test&.include?("test")
|
|
May 11, 2020 at 22:11 | comment | added | Luke Griffiths |
@Gary's advice holds if your goal is to minimize exceptions raised in this part of the code. That is not always the best goal. Generally if you expect a string at this point in the code and a value is nil instead, that implies something unexpected has occurred and an exception being raised is appropriate. If the existence of a nil here is unexpected, using to_s will hide the problem and increase the distance between the source and the detection of the problem, making it harder to debug.
|
|
S Aug 1, 2019 at 21:05 | history | suggested | Dave Powers | CC BY-SA 4.0 |
Fixed broken link
|
Aug 1, 2019 at 19:25 | review | Suggested edits | |||
S Aug 1, 2019 at 21:05 | |||||
May 8, 2018 at 21:24 | comment | added | Gary | Include on it's own is not ideal as it can throw a NoMethodError if nil test = nil test.include?("test") NoMethodError: undefined method `include?' for nil:NilClass should always convert the value being included to the expected value:- test.to_s.include?("test") | |
Jul 6, 2017 at 16:54 | history | rollback | Adam Lear♦ |
Rollback to Revision 1
|
|
Jul 6, 2017 at 11:14 | history | edited | Arslan Ali | CC BY-SA 3.0 |
A bit ruby way.
|
Mar 25, 2014 at 7:58 | comment | added | phortx |
Remember that include? is case sensetive. So if my_string in the example above would be something like "abcDefg" (with an uppercase D ), include?("cde") would return false . You may want to do a downcase() before calling include?() .
|
|
Nov 28, 2011 at 5:54 | vote | accept | BSalunke | ||
Nov 24, 2011 at 14:24 | history | answered | Adam Lear♦ | CC BY-SA 3.0 |