SlideShare a Scribd company logo
1 of 34
METHOD OF THE MONTH




 Ruby’s String Slicing
                      Kevin Munc
                      @muncman
Have you ever wanted a
         portion of a String?




COLUMBUS RUBY BRIGADE
“COLUMBUS RUBY
BRIGADE”.slice(5,8)
EXAMPLES
# The slice Method (start, length)
"abcdefghijklm".slice(5,3) => "fgh"
"0123456789".slice(5,3) => "567"
# The slice Method (start, length)
"abcdefghijklm".slice(5,3) => "fgh"
"0123456789".slice(5,3) => "567"

# Bracketed
"0123456789"[5,3] => "567"
# Ranges
"0123456789".slice(3..6) => "3456"
"0123456789".slice(3...6) => "345"
# Ranges
"0123456789".slice(3..6) => "3456"
"0123456789".slice(3...6) => "345"
"0123456789"[3..6] => "3456"
"0123456789"[3...6] => "345"
# With Negatives
"0123456789".slice(-4,3) => "678"
"0123456789".slice(-4..-2) => "678"
# With Negatives
"0123456789".slice(-4,3) => "678"
"0123456789".slice(-4..-2) => "678"
"0123456789".slice(5,-3) => nil
"0123456789".slice(-4..4) => ""
# With Negatives
"0123456789".slice(-4,3) => "678"
"0123456789".slice(-4..-2) => "678"
"0123456789".slice(5,-3) => nil
"0123456789".slice(-4..4) => ""
"0123456789"[10..10] => ""
"0123456789".[10,0] => ""
# Beyond Bounds
"0123456789".slice(8,100) => "89"
"0123456789".slice(12..14) => nil
# Regex
"0123456789"[/45/] => "45"
"0123456789".slice(/4.6/) => "456"
"0123456789".slice(/abc/) => nil
# With a single argument (index)
"0123456789".slice(0) => 48
"0123456789"[4] => 52
"0123456789".slice(10) => nil
# With a single argument (index)
"0123456789".slice(0) => 48
"0123456789"[4] => 52
"0123456789".slice(10) => nil
# ?4 => 52 and 52.chr => "4"
# With a single argument (index)
"0123456789".slice(0) => 48
"0123456789"[4] => 52
"0123456789".slice(10) => nil
# ?4 => 52 and 52.chr => "4"
# To get a single character
"0123456789".slice(4,1) => "4"
"0123456789"[3..3] => "3"
# With (sub-)Strings
"0123456789".slice("678") => "678"
"0123456789".slice("876") => nil
# With a Bang
seq = "0123456789"
    => "0123456789"
# With a Bang
seq = "0123456789"
    => "0123456789"
seq.slice(6..8) => "678"
seq => "0123456789"
# With a Bang
seq = "0123456789"
    => "0123456789"
seq.slice(6..8) => "678"
seq => "0123456789"
seq.slice!(6..8) => "678"
seq => "0123459"
# With a Bang, Progressively
bev = "orange juice"
    => "orange juice"
# With a Bang, Progressively
bev = "orange juice"
    => "orange juice"
bev.slice!("e") => "e"
bev => "orang juice"
# With a Bang, Progressively
bev = "orange juice"
    => "orange juice"
bev.slice!("e") => "e"
bev => "orang juice"
bev.slice!("e") => "e"
bev => "orang juic"
# With a Bang, Progressively
bev = "orange juice"
    => "orange juice"
bev.slice!("e") => "e"
bev => "orang juice"
bev.slice!("e") => "e"
bev => "orang juic"
bev.slice!("e") => nil
# Assignment, with Brackets Only
seq = "0123456789"
    => "0123456789"
# Assignment, with Brackets Only
seq = "0123456789"
    => "0123456789"
seq[0] = "a" => "a"
seq => "a123456789"
# Assignment, with Brackets Only
seq = "0123456789"
    => "0123456789"
seq[0] = "a" => "a"
seq => "a123456789"
seq[5,2] = "xy" => "xy"
seq => "a1234xy789"
# Assignment, continued
seq => "a1234xy789"
# Assignment, continued
seq => "a1234xy789"
seq[7,3] = "_" => "_"
seq => "a1234xy_"
# Assignment, continued
seq => "a1234xy789"
seq[7,3] = "_" => "_"
seq => "a1234xy_"
seq[3,2] = "LONG" => "LONG"
seq => "a12LONGxy_"
# Not just for Strings
myArray = [0,1,2,3,4,5,6,7,8,9]
   => [0,1,2,3,4,5,6,7,8,9]
# Not just for Strings
myArray = [0,1,2,3,4,5,6,7,8,9]
   => [0,1,2,3,4,5,6,7,8,9]
myArray.slice(2,3) => [2, 3, 4]
myArray[2..4] => [2, 3, 4]
myArray[0,5] = [9,8] => [9,8]
myArray => [9, 8, 5, 6, 7, 8, 9]
Try it yourself.
It’s completely
      safe!
Questions? Answers?
                       Photo Sources:
 http://www.flickr.com/photos/nickwheeleroz/2201057065/
    http://www.flickr.com/photos/simpologist/42391997/
 http://www.flickr.com/photos/uaeincredible/3234149448/
    http://www.flickr.com/photos/mzaluska/3313083808/

More Related Content

More from Kevin 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
 
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
 
Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)Kevin Munc
 
Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)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
 
gsub (MOTM 2009.09)
gsub (MOTM 2009.09)gsub (MOTM 2009.09)
gsub (MOTM 2009.09)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
 

More from Kevin Munc (11)

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’
 
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)
 
Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)
 
Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)
 
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)
 
gsub (MOTM 2009.09)
gsub (MOTM 2009.09)gsub (MOTM 2009.09)
gsub (MOTM 2009.09)
 
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)
 

Recently uploaded

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

Ruby's String Slicing (MOTM 2009.07)

  • 1. METHOD OF THE MONTH Ruby’s String Slicing Kevin Munc @muncman
  • 2. Have you ever wanted a portion of a String? COLUMBUS RUBY BRIGADE
  • 5. # The slice Method (start, length) "abcdefghijklm".slice(5,3) => "fgh" "0123456789".slice(5,3) => "567"
  • 6. # The slice Method (start, length) "abcdefghijklm".slice(5,3) => "fgh" "0123456789".slice(5,3) => "567" # Bracketed "0123456789"[5,3] => "567"
  • 7. # Ranges "0123456789".slice(3..6) => "3456" "0123456789".slice(3...6) => "345"
  • 8. # Ranges "0123456789".slice(3..6) => "3456" "0123456789".slice(3...6) => "345" "0123456789"[3..6] => "3456" "0123456789"[3...6] => "345"
  • 9. # With Negatives "0123456789".slice(-4,3) => "678" "0123456789".slice(-4..-2) => "678"
  • 10. # With Negatives "0123456789".slice(-4,3) => "678" "0123456789".slice(-4..-2) => "678" "0123456789".slice(5,-3) => nil "0123456789".slice(-4..4) => ""
  • 11. # With Negatives "0123456789".slice(-4,3) => "678" "0123456789".slice(-4..-2) => "678" "0123456789".slice(5,-3) => nil "0123456789".slice(-4..4) => "" "0123456789"[10..10] => "" "0123456789".[10,0] => ""
  • 12. # Beyond Bounds "0123456789".slice(8,100) => "89" "0123456789".slice(12..14) => nil
  • 13. # Regex "0123456789"[/45/] => "45" "0123456789".slice(/4.6/) => "456" "0123456789".slice(/abc/) => nil
  • 14. # With a single argument (index) "0123456789".slice(0) => 48 "0123456789"[4] => 52 "0123456789".slice(10) => nil
  • 15. # With a single argument (index) "0123456789".slice(0) => 48 "0123456789"[4] => 52 "0123456789".slice(10) => nil # ?4 => 52 and 52.chr => "4"
  • 16. # With a single argument (index) "0123456789".slice(0) => 48 "0123456789"[4] => 52 "0123456789".slice(10) => nil # ?4 => 52 and 52.chr => "4" # To get a single character "0123456789".slice(4,1) => "4" "0123456789"[3..3] => "3"
  • 17. # With (sub-)Strings "0123456789".slice("678") => "678" "0123456789".slice("876") => nil
  • 18. # With a Bang seq = "0123456789" => "0123456789"
  • 19. # With a Bang seq = "0123456789" => "0123456789" seq.slice(6..8) => "678" seq => "0123456789"
  • 20. # With a Bang seq = "0123456789" => "0123456789" seq.slice(6..8) => "678" seq => "0123456789" seq.slice!(6..8) => "678" seq => "0123459"
  • 21. # With a Bang, Progressively bev = "orange juice" => "orange juice"
  • 22. # With a Bang, Progressively bev = "orange juice" => "orange juice" bev.slice!("e") => "e" bev => "orang juice"
  • 23. # With a Bang, Progressively bev = "orange juice" => "orange juice" bev.slice!("e") => "e" bev => "orang juice" bev.slice!("e") => "e" bev => "orang juic"
  • 24. # With a Bang, Progressively bev = "orange juice" => "orange juice" bev.slice!("e") => "e" bev => "orang juice" bev.slice!("e") => "e" bev => "orang juic" bev.slice!("e") => nil
  • 25. # Assignment, with Brackets Only seq = "0123456789" => "0123456789"
  • 26. # Assignment, with Brackets Only seq = "0123456789" => "0123456789" seq[0] = "a" => "a" seq => "a123456789"
  • 27. # Assignment, with Brackets Only seq = "0123456789" => "0123456789" seq[0] = "a" => "a" seq => "a123456789" seq[5,2] = "xy" => "xy" seq => "a1234xy789"
  • 28. # Assignment, continued seq => "a1234xy789"
  • 29. # Assignment, continued seq => "a1234xy789" seq[7,3] = "_" => "_" seq => "a1234xy_"
  • 30. # Assignment, continued seq => "a1234xy789" seq[7,3] = "_" => "_" seq => "a1234xy_" seq[3,2] = "LONG" => "LONG" seq => "a12LONGxy_"
  • 31. # Not just for Strings myArray = [0,1,2,3,4,5,6,7,8,9] => [0,1,2,3,4,5,6,7,8,9]
  • 32. # Not just for Strings myArray = [0,1,2,3,4,5,6,7,8,9] => [0,1,2,3,4,5,6,7,8,9] myArray.slice(2,3) => [2, 3, 4] myArray[2..4] => [2, 3, 4] myArray[0,5] = [9,8] => [9,8] myArray => [9, 8, 5, 6, 7, 8, 9]
  • 33. Try it yourself. It’s completely safe!
  • 34. Questions? Answers? Photo Sources: http://www.flickr.com/photos/nickwheeleroz/2201057065/ http://www.flickr.com/photos/simpologist/42391997/ http://www.flickr.com/photos/uaeincredible/3234149448/ http://www.flickr.com/photos/mzaluska/3313083808/

Editor's Notes

  1. My name... Twitter... First MOTM... Hope to lower the bar: don’t need big topic, deep insights or vast experience... In spirit of more beginner friendly topics... Just ONE method. Encourage IRB-ing-along... Stop me if want to see a slide longer... (Encourage questions and answers from audience...) Don’t need to talk about hot deploying distributed Camping apps from a Merb app using Sinatra. TODO: slide with can and tomato (Ginsu reference) ?
  2. I’ll cover aspects of the slice method in the following examples.
  3. Primary version. Take time here... PAUSE.
  4. Can’t have a negative length. Beware strange behavior at the end of Strings. 10 here is a valid range, but not a valid individual index (see slide 10). Remember to PAUSE... NOTE: http://www.nabble.com/ruby-string-slice----w--range,-weird-end-behavior-td23455258.html A Ruby string is not a *char[] and the index points are intersticies _between_ an array of characters, not the addresses of those characters.Half steps, fence posts... (There is an index, but no value?)
  5. Can’t have a negative length. Beware strange behavior at the end of Strings. 10 here is a valid range, but not a valid individual index (see slide 10). Remember to PAUSE... NOTE: http://www.nabble.com/ruby-string-slice----w--range,-weird-end-behavior-td23455258.html A Ruby string is not a *char[] and the index points are intersticies _between_ an array of characters, not the addresses of those characters.Half steps, fence posts... (There is an index, but no value?)
  6. If your length argument is longer than the string remainder, you get up to the end.
  7. No matches result in nil results. Remember to PAUSE...
  8. Character codes... See slide 7 regarding index 10 versus range 10. Halfway. Take time...
  9. Character codes... See slide 7 regarding index 10 versus range 10. Halfway. Take time...
  10. Remember to PAUSE...
  11. Remember to PAUSE...
  12. Remember to PAUSE...
  13. Remember to PAUSE...
  14. The size of the slice and its replacement don’t have to match. Shorter, and longer... Remember to PAUSE...
  15. The size of the slice and its replacement don’t have to match. Shorter, and longer... Remember to PAUSE...
  16. Finally...
  17. Try it! Even with the “Slice Bang!” IRB, unit test...
  18. Answers from the audience are appreciated, as well. Any slides people want to see again?