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]
Written by Corey Grunewald
Related protips
2 Responses
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#