I am working with an array of midi steps that looks like this ...
pitches = [ 60, nil, nil, nil, 67, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil ]
In this case, the step is still 60 at indices 1, 2, and 3.
Following index 4, step another 67.
How can I write a method to determine the previous value other than zero?
The only way I can do it now seems a bit awkward:
def pitch_at_step(pitches,step) if pitches.any? x = pitches[step] until x != nil index -= 1 x = pitches[step] end x else nil end end
The expected output is in the format:
pitch_at_step(pitches, 0)
Is this the best solution? Is there a more accurate and / or more efficient way?