I’m trying to perform an operation on each character of a string which is followed by another character, i.e. one that is not the last character. My code is:
[0..(str.length - 2)].each do |index|
// do something with index
end
The code errors out. [0..(str.length - 2)] returns a range object rather than an actual range which can be iterated over. Putting a return index in the body of the above causes a single iteration, with an output of something like [0..7]. Why isn’t this iterating over each number?
By enclosing the range in [..], you have created an Array containing a single Range object. In other words, the length of your Array is one. You just want: