find the first index of a substring in a string in ruby

To find the first index of a substring in a string in Ruby, you can use the index method of the string class. Here's an example:

main.rb
my_string = "hello world"
substring = "world"
index = my_string.index(substring)
puts index # outputs 6
104 chars
5 lines

In the code above, we create a string called my_string and a substring called substring. We then use the index method to find the first index of the substring in the string and assign it to a variable called index. Finally, we print the value of the index variable, which should output 6.

gistlibby LogSnag