Info Whenever we call index and rindex on a string, we should have code that checks for a nil result.
Detail We call index and the argument is not found in the string. The code detects this and prints a message.
Detail We invoke rindex and the argument is not found—the code prints a message to report this.
input = "test"# Part A: test index() for nil result.
result1 = input.index("x")
puts "NOT FOUND 1" if result1 == nil# Part B: test rindex() for nil result.
result2 = input.rindex("x")
puts "NOT FOUND 2" if result2 == nilNOT FOUND 1
NOT FOUND 2
Between, before, after. In developing more complex Ruby methods, like a between method, we must use index() and rindex to search a string. And we should be careful with a nil result.