To find the first index of a character in a string in Ruby, you can use the index
method. Here's an example:
main.rb84 chars5 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