Your SlideShare is downloading. ×
0
MOTM: Take & Drop
Kevin Munc — @muncman
Taking
take & take_while

take(n) gives you the first n elements

take_while { |elem| block } gives you
elements until the block ...
take
uhray = [0,1,2,3,4,5,6,7,8,9]
uhray.take(5)
=> [0,1,2,3,4]
take
uhray = [0,1,2,3,4,5,6,7,8,9]
uhray.take(5)
=> [0,1,2,3,4]


uhray = []
uhray.take(5)
=> []
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]
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|...
Dropping
drop & drop_while

drop(n) gives you all elements after the first
n elements

drop_while { |elem| block } gives you all
el...
drop
uhray = [0,1,2,3,4,5,6,7,8,9]
uhray.drop(5)
=> [5,6,7,8,9]
drop
uhray = [0,1,2,3,4,5,6,7,8,9]
uhray.drop(5)
=> [5,6,7,8,9]


uhray = []
uhray.drop(5)
=> []
drop_while
uhray = [0,1,2,3,4,5,6,7,8,9]
uhray.drop_while { |item| item < 6 }
=> [6,7,8,9]
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| ite...
Questions?
                 Built on an iPad



                    Photo Credits
http://www.flickr.com/photos/stevewall/80...
Upcoming SlideShare
Loading in...5
×

Take & Drop (MOTM 2010.04)

364

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
364
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide










  • Transcript of "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/
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×