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
"0123...
# 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...
# 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
"01...
# With Negatives
"0123456789".slice(-4,3) => "678"
"0123456789".slice(-4..-2) => "678"
"0123456789".slice(5,-3) => nil
"01...
# 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 => 5...
# With a single argument (index)
"0123456789".slice(0) => 48
"0123456789"[4] => 52
"0123456789".slice(10) => nil
# ?4 => 5...
# 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...
# 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.sl...
# With a Bang, Progressively
bev = "orange juice"
    => "orange juice"
bev.slice!("e") => "e"
bev => "orang juice"
bev.sl...
# 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] =...
# 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 => "a1...
# 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...
Try it yourself.
It’s completely
      safe!
Questions? Answers?
                       Photo Sources:
 http://www.flickr.com/photos/nickwheeleroz/2201057065/
    http:...
Upcoming SlideShare
Loading in …5
×

Ruby's String Slicing (MOTM 2009.07)

2,597 views

Published on

String Slicing. Ruby Method of the Month 2009.07. 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
2,597
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 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) ?

  • I’ll cover aspects of the slice method in the following examples.

  • Primary version.
    Take time here... PAUSE.


  • 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?)

  • 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?)

  • If your length argument is longer than the string remainder, you get up to the end.

  • No matches result in nil results.
    Remember to PAUSE...
  • Character codes...
    See slide 7 regarding index 10 versus range 10.
    Halfway. Take time...
  • Character codes...
    See slide 7 regarding index 10 versus range 10.
    Halfway. Take time...
  • Remember to PAUSE...


  • Remember to PAUSE...
  • Remember to PAUSE...
  • Remember to PAUSE...


  • The size of the slice and its replacement don’t have to match.
    Shorter, and longer...
    Remember to PAUSE...
  • The size of the slice and its replacement don’t have to match.
    Shorter, and longer...
    Remember to PAUSE...
  • Finally...
  • Try it!
    Even with the “Slice Bang!”
    IRB, unit test...
  • Answers from the audience are appreciated, as well.
    Any slides people want to see again?
  • Ruby's String Slicing (MOTM 2009.07)

    1. 1. METHOD OF THE MONTH Ruby’s String Slicing Kevin Munc @muncman
    2. 2. Have you ever wanted a portion of a String? COLUMBUS RUBY BRIGADE
    3. 3. “COLUMBUS RUBY BRIGADE”.slice(5,8)
    4. 4. EXAMPLES
    5. 5. # The slice Method (start, length) "abcdefghijklm".slice(5,3) => "fgh" "0123456789".slice(5,3) => "567"
    6. 6. # The slice Method (start, length) "abcdefghijklm".slice(5,3) => "fgh" "0123456789".slice(5,3) => "567" # Bracketed "0123456789"[5,3] => "567"
    7. 7. # Ranges "0123456789".slice(3..6) => "3456" "0123456789".slice(3...6) => "345"
    8. 8. # Ranges "0123456789".slice(3..6) => "3456" "0123456789".slice(3...6) => "345" "0123456789"[3..6] => "3456" "0123456789"[3...6] => "345"
    9. 9. # With Negatives "0123456789".slice(-4,3) => "678" "0123456789".slice(-4..-2) => "678"
    10. 10. # With Negatives "0123456789".slice(-4,3) => "678" "0123456789".slice(-4..-2) => "678" "0123456789".slice(5,-3) => nil "0123456789".slice(-4..4) => ""
    11. 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. 12. # Beyond Bounds "0123456789".slice(8,100) => "89" "0123456789".slice(12..14) => nil
    13. 13. # Regex "0123456789"[/45/] => "45" "0123456789".slice(/4.6/) => "456" "0123456789".slice(/abc/) => nil
    14. 14. # With a single argument (index) "0123456789".slice(0) => 48 "0123456789"[4] => 52 "0123456789".slice(10) => nil
    15. 15. # With a single argument (index) "0123456789".slice(0) => 48 "0123456789"[4] => 52 "0123456789".slice(10) => nil # ?4 => 52 and 52.chr => "4"
    16. 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. 17. # With (sub-)Strings "0123456789".slice("678") => "678" "0123456789".slice("876") => nil
    18. 18. # With a Bang seq = "0123456789" => "0123456789"
    19. 19. # With a Bang seq = "0123456789" => "0123456789" seq.slice(6..8) => "678" seq => "0123456789"
    20. 20. # With a Bang seq = "0123456789" => "0123456789" seq.slice(6..8) => "678" seq => "0123456789" seq.slice!(6..8) => "678" seq => "0123459"
    21. 21. # With a Bang, Progressively bev = "orange juice" => "orange juice"
    22. 22. # With a Bang, Progressively bev = "orange juice" => "orange juice" bev.slice!("e") => "e" bev => "orang juice"
    23. 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. 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. 25. # Assignment, with Brackets Only seq = "0123456789" => "0123456789"
    26. 26. # Assignment, with Brackets Only seq = "0123456789" => "0123456789" seq[0] = "a" => "a" seq => "a123456789"
    27. 27. # Assignment, with Brackets Only seq = "0123456789" => "0123456789" seq[0] = "a" => "a" seq => "a123456789" seq[5,2] = "xy" => "xy" seq => "a1234xy789"
    28. 28. # Assignment, continued seq => "a1234xy789"
    29. 29. # Assignment, continued seq => "a1234xy789" seq[7,3] = "_" => "_" seq => "a1234xy_"
    30. 30. # Assignment, continued seq => "a1234xy789" seq[7,3] = "_" => "_" seq => "a1234xy_" seq[3,2] = "LONG" => "LONG" seq => "a12LONGxy_"
    31. 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. 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. 33. Try it yourself. It’s completely safe!
    34. 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/

    ×