Ruby's String Slicing (MOTM 2009.07)
Upcoming SlideShare
Loading in...5
×
 

Ruby's String Slicing (MOTM 2009.07)

on

  • 2,083 views

String Slicing. Ruby Method of the Month 2009.07. Lightning talk for the Columbus Ruby Brigade.

String Slicing. Ruby Method of the Month 2009.07. Lightning talk for the Columbus Ruby Brigade.

Statistics

Views

Total Views
2,083
Views on SlideShare
2,076
Embed Views
7

Actions

Likes
0
Downloads
1
Comments
0

3 Embeds 7

http://www.linkedin.com 4
http://www.linkedin-ei2.com 2
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • My name... Twitter... First MOTM... <br /> Hope to lower the bar: don&#x2019;t need big topic, deep insights or vast experience... <br /> In spirit of more beginner friendly topics... Just ONE method. <br /> Encourage IRB-ing-along... Stop me if want to see a slide longer... <br /> (Encourage questions and answers from audience...) <br /> <br /> Don&#x2019;t need to talk about hot deploying distributed Camping apps from a Merb app using Sinatra. <br /> TODO: slide with can and tomato (Ginsu reference) ? <br />
  • <br />
  • I&#x2019;ll cover aspects of the slice method in the following examples. <br />
  • <br />
  • Primary version. <br /> Take time here... PAUSE. <br />
  • <br />
  • <br />
  • Can&#x2019;t have a negative length. <br /> Beware strange behavior at the end of Strings. <br /> 10 here is a valid range, but not a valid individual index (see slide 10). <br /> Remember to PAUSE... <br /> <br /> NOTE: http://www.nabble.com/ruby-string-slice----w--range,-weird-end-behavior-td23455258.html <br /> 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?) <br /> <br />
  • Can&#x2019;t have a negative length. <br /> Beware strange behavior at the end of Strings. <br /> 10 here is a valid range, but not a valid individual index (see slide 10). <br /> Remember to PAUSE... <br /> <br /> NOTE: http://www.nabble.com/ruby-string-slice----w--range,-weird-end-behavior-td23455258.html <br /> 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?) <br /> <br />
  • If your length argument is longer than the string remainder, you get up to the end. <br /> <br />
  • No matches result in nil results. <br /> Remember to PAUSE... <br />
  • Character codes... <br /> See slide 7 regarding index 10 versus range 10. <br /> Halfway. Take time... <br />
  • Character codes... <br /> See slide 7 regarding index 10 versus range 10. <br /> Halfway. Take time... <br />
  • Remember to PAUSE... <br />
  • <br />
  • <br />
  • Remember to PAUSE... <br />
  • Remember to PAUSE... <br />
  • Remember to PAUSE... <br />
  • <br />
  • <br />
  • The size of the slice and its replacement don&#x2019;t have to match. <br /> Shorter, and longer... <br /> Remember to PAUSE... <br />
  • The size of the slice and its replacement don&#x2019;t have to match. <br /> Shorter, and longer... <br /> Remember to PAUSE... <br />
  • Finally... <br />
  • Try it! <br /> Even with the &#x201C;Slice Bang!&#x201D; <br /> IRB, unit test... <br />
  • Answers from the audience are appreciated, as well. <br /> Any slides people want to see again? <br />

Ruby's String Slicing (MOTM 2009.07) Ruby's String Slicing (MOTM 2009.07) Presentation Transcript

  • 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/