Twitter Updates

    follow me on Twitter

    Your Not-So-Free Lunch


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

    December 06, 2006

    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 saved. Comments are moderated and will not appear until approved by the author. 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

    Comments are moderated, and will not appear until the author has approved them.