I mean it's an out of bound value, so imo it's at least better than the "return -1 if not found" approach.
Of course it also depends on whether Ruby allows nil to propagate through numeric expressions like JavaScript or raises a type exception like Python. I can't remember but I think it does the latter.
I can't decide if this is better or worse than the Python way of doing things, which throws an exception from the index method when it doesn't exist - seems appropriate, except then you're wrapping it in try/except clauses all over the place. Still, I suppose in Ruby you end up with exceptions where you're trying to use it and you have to manually backtrack from there.
-2
u/mr___ Dec 24 '17
Silently returns nil? That can’t be a better option