SlideShare ist ein Scribd-Unternehmen logo
1 von 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/

Weitere ähnliche Inhalte

Mehr von 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
 

Mehr von 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)
 

Kürzlich hochgeladen

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
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
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
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
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
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 

Kürzlich hochgeladen (20)

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?
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
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...
 
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...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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)
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 

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/

Hinweis der Redaktion

  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?