Int32 or Nil
text = "Black cat"
idx = text.index("cat")
puts typeof(idx) # (Int32 | Nil)
# puts text[idx]
# Error: no overload matches 'String#[]' with type (Int32 | Nil)
if idx.is_a?(Int32)
puts typeof(idx) # Int32
puts text[idx]
end
Press ← or → to navigate between chapters
Press S or / to search in the book
Press ? to show this help
Press Esc to hide this help
text = "Black cat"
idx = text.index("cat")
puts typeof(idx) # (Int32 | Nil)
# puts text[idx]
# Error: no overload matches 'String#[]' with type (Int32 | Nil)
if idx.is_a?(Int32)
puts typeof(idx) # Int32
puts text[idx]
end