Take & Drop (MOTM 2010.04)

498 views

Published on

Ruby Method of the Month 2010.04. Lightning talk for the Columbus Ruby Brigade.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
498
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide










  • Take & Drop (MOTM 2010.04)

    1. 1. MOTM: Take & Drop Kevin Munc — @muncman
    2. 2. Taking
    3. 3. take & take_while take(n) gives you the first n elements take_while { |elem| block } gives you elements until the block condition is nil or false Works on Array and Enumerable
    4. 4. take uhray = [0,1,2,3,4,5,6,7,8,9] uhray.take(5) => [0,1,2,3,4]
    5. 5. take uhray = [0,1,2,3,4,5,6,7,8,9] uhray.take(5) => [0,1,2,3,4] uhray = [] uhray.take(5) => []
    6. 6. take_while uhray = [0,1,2,3,4,5,6,7,8,9] uhray.take_while { |item| item < 6 } => [0,1,2,3,4,5]
    7. 7. take_while uhray = [0,1,2,3,4,5,6,7,8,9] uhray.take_while { |item| item < 6 } => [0,1,2,3,4,5] uhray.take_while { |item| item > 6 } => []
    8. 8. Dropping
    9. 9. drop & drop_while drop(n) gives you all elements after the first n elements drop_while { |elem| block } gives you all elements starting when the block condition is nil or false Works on Array and Enumerable
    10. 10. drop uhray = [0,1,2,3,4,5,6,7,8,9] uhray.drop(5) => [5,6,7,8,9]
    11. 11. drop uhray = [0,1,2,3,4,5,6,7,8,9] uhray.drop(5) => [5,6,7,8,9] uhray = [] uhray.drop(5) => []
    12. 12. drop_while uhray = [0,1,2,3,4,5,6,7,8,9] uhray.drop_while { |item| item < 6 } => [6,7,8,9]
    13. 13. drop_while uhray = [0,1,2,3,4,5,6,7,8,9] uhray.drop_while { |item| item < 6 } => [6,7,8,9] uhray.drop_while { |item| item > 6 } => [0,1,2,3,4,5,6,7,8,9]
    14. 14. Questions? Built on an iPad Photo Credits http://www.flickr.com/photos/stevewall/802936876/ http://www.flickr.com/photos/z6p6tist6/3967592710/ http://www.flickr.com/photos/whosdadog/3652663745/

    ×