'count the distance between to strings in an array Ruby

i have an array

line_one = ["flinders street", "richmond", "east richmond", "burnley", "hawthorn", "glenferrie"]

user_input1 = "flinders street"
user_input2 = "glenferrie"

how could I count the distance between the two strings? expected output 5.



Solution 1:[1]

line_one = ["flinders street", "richmond", "east richmond", "burnley", "hawthorn", "glenferrie"]

Code

p (line_one.index("flinders street")...line_one.index("glenferrie")).count

output

5

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1