SlideShare una empresa de Scribd logo
1 de 14
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 condition is nil or
false

Works on Array and Enumerable
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| item > 6 }
=> []
Dropping
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
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| item > 6 }
=> [0,1,2,3,4,5,6,7,8,9]
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/

Más contenido relacionado

La actualidad más candente

C4 4.5
C4 4.5C4 4.5
C4 4.5BGEsp1
 
Solving compound inequalities
Solving compound inequalitiesSolving compound inequalities
Solving compound inequalitiesLyndeeGovert
 
Multiplying and-dividingfor web
Multiplying and-dividingfor webMultiplying and-dividingfor web
Multiplying and-dividingfor webMs. Jones
 
Aprendiendo jugando
Aprendiendo jugandoAprendiendo jugando
Aprendiendo jugandoPAULA
 
POTENCIAS Y RADICALES
POTENCIAS Y RADICALESPOTENCIAS Y RADICALES
POTENCIAS Y RADICALESEducación
 
Pengolahan data
Pengolahan dataPengolahan data
Pengolahan dataMatt Gani
 
JUEC 初中 (Circle)
JUEC 初中 (Circle)JUEC 初中 (Circle)
JUEC 初中 (Circle)tungwc
 
Tugas kalkulus
Tugas kalkulusTugas kalkulus
Tugas kalkulusFajar Corp
 
Binary search: illustrated step-by-step walk through
Binary search: illustrated step-by-step walk throughBinary search: illustrated step-by-step walk through
Binary search: illustrated step-by-step walk throughYoshi Watanabe
 
8 points on the unit circle the wrapping function w(t)
8 points on the unit circle  the wrapping function w(t)8 points on the unit circle  the wrapping function w(t)
8 points on the unit circle the wrapping function w(t)Anthony Kuznetsov
 
Square root of any number(5)
Square root of any number(5)Square root of any number(5)
Square root of any number(5)Neel Patel
 
師父與徒弟的列式
師父與徒弟的列式師父與徒弟的列式
師父與徒弟的列式庭蘭 李
 
師父與徒弟的列式
師父與徒弟的列式師父與徒弟的列式
師父與徒弟的列式庭蘭 李
 
A Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoA Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoChung Hua Universit
 

La actualidad más candente (19)

C4 4.5
C4 4.5C4 4.5
C4 4.5
 
Solving compound inequalities
Solving compound inequalitiesSolving compound inequalities
Solving compound inequalities
 
Multiplying and-dividingfor web
Multiplying and-dividingfor webMultiplying and-dividingfor web
Multiplying and-dividingfor web
 
Aprendiendo jugando
Aprendiendo jugandoAprendiendo jugando
Aprendiendo jugando
 
Data types
Data typesData types
Data types
 
POTENCIAS Y RADICALES
POTENCIAS Y RADICALESPOTENCIAS Y RADICALES
POTENCIAS Y RADICALES
 
Pengolahan data
Pengolahan dataPengolahan data
Pengolahan data
 
JUEC 初中 (Circle)
JUEC 初中 (Circle)JUEC 初中 (Circle)
JUEC 初中 (Circle)
 
Tugas kalkulus
Tugas kalkulusTugas kalkulus
Tugas kalkulus
 
Chinese
ChineseChinese
Chinese
 
Binary search: illustrated step-by-step walk through
Binary search: illustrated step-by-step walk throughBinary search: illustrated step-by-step walk through
Binary search: illustrated step-by-step walk through
 
8 points on the unit circle the wrapping function w(t)
8 points on the unit circle  the wrapping function w(t)8 points on the unit circle  the wrapping function w(t)
8 points on the unit circle the wrapping function w(t)
 
Square root of any number(5)
Square root of any number(5)Square root of any number(5)
Square root of any number(5)
 
Hw5
Hw5Hw5
Hw5
 
師父與徒弟的列式
師父與徒弟的列式師父與徒弟的列式
師父與徒弟的列式
 
師父與徒弟的列式
師父與徒弟的列式師父與徒弟的列式
師父與徒弟的列式
 
Example feedforward backpropagation
Example feedforward backpropagationExample feedforward backpropagation
Example feedforward backpropagation
 
Latihan 3.3
Latihan 3.3Latihan 3.3
Latihan 3.3
 
A Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoA Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter two
 

Destacado

Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)Kevin Munc
 
gsub (MOTM 2009.09)
gsub (MOTM 2009.09)gsub (MOTM 2009.09)
gsub (MOTM 2009.09)Kevin Munc
 
NaN, Zero, & Infinities
NaN, Zero, & InfinitiesNaN, Zero, & Infinities
NaN, Zero, & InfinitiesKevin Munc
 
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’Kevin Munc
 
Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013Kevin Munc
 
Lookin Out - Program Overview
Lookin Out - Program OverviewLookin Out - Program Overview
Lookin Out - Program Overviewmbresee
 

Destacado (6)

Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)
 
gsub (MOTM 2009.09)
gsub (MOTM 2009.09)gsub (MOTM 2009.09)
gsub (MOTM 2009.09)
 
NaN, Zero, & Infinities
NaN, Zero, & InfinitiesNaN, Zero, & Infinities
NaN, Zero, & Infinities
 
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
 
Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013
 
Lookin Out - Program Overview
Lookin Out - Program OverviewLookin Out - Program Overview
Lookin Out - Program Overview
 

Similar a Take & Drop (MOTM 2010.04)

Similar a Take & Drop (MOTM 2010.04) (7)

AA_Sorting.SI.ppt
AA_Sorting.SI.pptAA_Sorting.SI.ppt
AA_Sorting.SI.ppt
 
Correct sorting with Frama-C
Correct sorting with Frama-CCorrect sorting with Frama-C
Correct sorting with Frama-C
 
IA-advanced-R
IA-advanced-RIA-advanced-R
IA-advanced-R
 
Slides ads ia
Slides ads iaSlides ads ia
Slides ads ia
 
DSSchapt13.ppt
DSSchapt13.pptDSSchapt13.ppt
DSSchapt13.ppt
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
 
Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
 

Más de Kevin Munc

Basic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setIntervalBasic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setIntervalKevin Munc
 
Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Kevin Munc
 
Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)Kevin Munc
 
cycle (MOTM 2010.07)
cycle (MOTM 2010.07)cycle (MOTM 2010.07)
cycle (MOTM 2010.07)Kevin Munc
 
empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)Kevin Munc
 
Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Kevin Munc
 
Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)Kevin Munc
 
The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)Kevin Munc
 
Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Kevin Munc
 

Más de Kevin Munc (10)

Basic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setIntervalBasic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setInterval
 
Shellwords
ShellwordsShellwords
Shellwords
 
Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)
 
Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)
 
cycle (MOTM 2010.07)
cycle (MOTM 2010.07)cycle (MOTM 2010.07)
cycle (MOTM 2010.07)
 
empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)
 
Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)
 
Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)
 
The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)
 
Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)
 

Último

Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 

Último (20)

Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Take & Drop (MOTM 2010.04)

Notas del editor