o1zqvw
Last Updated: February 25, 2016
·
2.519K
· 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

4341

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

over 1 year ago ·
4342

@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 ·