RbRuby · Lesson 5 of 11
Arrays, Hashes & Ranges
Ruby's collection classes are loaded with useful methods. You'll rarely need to write a loop — there's almost always a method for what you're trying to do.
✦ Tip
`map` returns a new array. `each` returns the original. `select` keeps matches. `reject` drops matches. `reduce` collapses to a single value. When in doubt: if you need a new array, use `map` or `select`. If you just want a side effect (printing, writing), use `each`.
`map` returns a new array. `each` returns the original. `select` keeps matches. `reject` drops matches. `reduce` collapses to a single value. When in doubt: if you need a new array, use `map` or `select`. If you just want a side effect (printing, writing), use `each`.