o1zqvw
Last Updated: February 25, 2016
·
1.638K
· coreygrunewald
F5b375690fefcb29c25f2cfbb3286f19

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]

Say Thanks
Respond

2 Responses
Add your response

4341
Me

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

over 1 year ago ·
4342
F5b375690fefcb29c25f2cfbb3286f19

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