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

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

main.rb
string = "hello world"
char = "o"
index = string.index(char)
puts index # output: 4
84 chars
5 lines

In this example, we define a string hello world and the character we want to find the index of is o. We then call the index method on the string, passing in the character. The index method returns the first occurrence of the character in the string, or nil if the character is not found. So in this case, the output is 4, which is the index of the first occurrence of o in the string.

gistlibby LogSnag