« Ruby Refactoring Pattern: "Forward with Default Params" | Main | Resolving Files with TextMate, Subversion, and FileMerge »

December 06, 2006

Ruby Arrays: select(), collect(), and map()

As a former Java developer and new to the concept of blocks, these API options seem perplexing at first.   They're not.  All three take a block.  How they use the block is what distinguishes them.  You should read the RDoc for yourself but, if you want a quick a dirty summary, continue reading this post.

collect {|item|block} and map{|item|block} do the same thing - return an array of things returned by the block.  This is different from returning specific items in the collection being iterated over. 

Which leads me to select

select{|item|block} will return actual collection items being iterated over if, for each item, the block condition evaluates to true. Not the same as returning what the block, itself, may return.  In the case of select, the block would always return an instance of class TrueClass or FalseClass.  Typically, [true, false, ..., true] is not what you're looking for in your resulting array.

Slightly modifying the core RDoc example:

a = ["a", "b", "c", "d"]      #=> ["a", "b", "c", "d"]
a.map {|item|"a" == item}     #=> [true, false, false, false]
a.select {|item|"a" == item}  #=> ["a"]

Happy coding!

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83455a82969e200d834eb739b53ef

Listed below are links to weblogs that reference Ruby Arrays: select(), collect(), and map():

Comments

Thanks for the Array#select example! _m2

Hey Ben,

I searched for "ruby .collect" and your site was the first result, pretty cool :-)

ttyl, Ammar

Ive been to your site like 20 times in the past year or so. Very straightforward simple concise with example. GOOD JOB.

a=["a","b","c","d","e"]
=> ["a", "b", "c", "d", "e"]
irb(main):002:0> a.map{|i| "a"==i}
=> [true, false, false, false, false]
irb(main):003:0> a.select{|i|"a"==i}
=> ["a"]
irb(main):004:0> a.collect{|i|"a"==i}
=> [true, false, false, false, false]
irb(main):005:0> b=a.map{|i| "a"==i}
=> [true, false, false, false, false]
irb(main):006:0> print b
truefalsefalsefalsefalse=> nil
irb(main):007:0> b=a.select{|i|"a"==i}
=> ["a"]
irb(main):008:0> print b
a=> nil
irb(main):009:0> b=a.collect{|i|"a"==i}
=> [true, false, false, false, false]
irb(main):010:0> print b
truefalsefalsefalsefalse=> nil
irb(main):011:0> b=a.select{|i|"a"+i}
=> ["a", "b", "c", "d", "e"]
irb(main):012:0> b=a.collect{|i|"a"+i}
=> ["aa", "ab", "ac", "ad", "ae"]
irb(main):013:0> b=a.map{|i| "a"+i}
=> ["aa", "ab", "ac", "ad", "ae"]
irb(main):014:0>

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment