Last Updated: February 25, 2016
·
4.938K
· coreygrunewald

Using .reject with .map in Ruby

Useful if for some reason you want to parse down a collection.

[1,2,3,4,5].reject{|x| x == 1}.map{|x| x }

=> [2,3,4,5]

2 Responses
Add your response

Why are you using #map? #to_a is much better.

over 1 year ago ·

@hauleth This is just an abstract example. Ideally if you just only wanted to remove items from a collection, you could just use .reject and it will return a new array. Of course you would only use map if you wanted transform the collection in some way.

over 1 year ago ·