each_with_index was previously introduced in Ruby. with_index was introduced later:
- to ensure wider use with various meters.
- so that the index starts with a number other than
0 .
Today, using with_index would be better in terms of generality and readability, but in terms of speeding up code, each_with_index works a little faster than each.with_index .
When you feel that one method can be easily expressed by a simple chain of several methods, it usually happens that a single method is faster than a chain. As for another example of this, reverse_each is faster than reverse.each . These methods have reason to exist.
sawa Nov 28 '13 at 5:47 on 2013-11-28 05:47
source share