Class 3<br />Strings and Symbols<br />
Strings<br />“I am a string!”<br />
Quoting Strings<br />Double quotes interpolate <br />&gt;&gt; me = &quot;double quotes are awesome”<br />&gt;&gt; &quot;I ...
Concatenating Strings<br />first_name = &quot;Yukihiro”<br />last_name = &quot;Matsumoto”<br />full_name = first_name + &q...
String Interpolation<br />birthday = “January 5th”<br />“My birthday is #{birthday}”<br />
String Formatting: upcase<br />&gt;&gt; full_name.upcase<br />=&gt; &quot;YUKIHIRO MATSUMOTO”<br />&gt;&gt; full_name<br /...
More String Formatting<br />&gt;&gt; full_name.downcase!<br />=&gt; &quot;yukihiro matsumoto&quot;<br />&gt;&gt; full_name...
Access a String’s Characters<br />&gt;&gt; full_name[2,4]<br />=&gt; &quot;kihi&quot;<br />&gt;&gt; full_name[4..6]<br />=...
Ruby 1.8<br />&gt;&gt; name = &quot;Yukihiro”<br />=&gt; &quot;Yukihiro”<br />&gt;&gt; name[4]<br />=&gt; 104<br />&gt;&gt...
Ruby 1.9<br />&gt;&gt; name = &quot;yukihiro”<br />=&gt; &quot;yukihiro”<br />&gt;&gt; name[4]<br />=&gt; &quot;h”<br />&g...
Modifying a String<br />&gt;&gt; full_name.slice!(&quot;hi&quot;)<br />=&gt; &quot;hi&quot;<br />&gt;&gt; full_name<br />=...
Query a String<br />&gt;&gt; full_name.include?(&quot;hi&quot;)<br />=&gt; true<br />&gt;&gt; full_name.empty?<br />=&gt; ...
Iterate Over a String<br />each_line: process each line in a string<br />haiku = &quot;5
7
5
”<br />haiku.each_line{|line|...
each_char: process charactercareful of 1.8.x and 1.9 differences</li></ul>&gt;&gt; word = &quot;こにちわ&quot;<br />=&gt; &quo...
Iterate Using Split<br />returns an array of partial strings exploded at a separator<br />secret_code = &quot;the black do...
Symbols<br />:i_am_a_symbol<br />
What is a Symbol?<br />A symbol represents a name.<br />Instances of the built-in class Symbol.<br />They efficiently desc...
A Symbol is not a String<br />	:thing != “thing”<br />However a symbol can be create from a string:<br />“thing”.to_sym<br...
Symbols are Immutable<br />You can’t change a symbol<br />For example, you can’t append characters to a symbol...once a sy...
Symbols are Unique<br />:name is the only symbol object called :name<br />&gt;&gt; :name.object_id<br />=&gt; 68828<br />&...
Upcoming SlideShare
Loading in...5
×

3 Strings Symbols

394

Published on

Published in: Sports, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
394
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • There is no title case method in ruby...we can make our own
  • Transcript of "3 Strings Symbols"

    1. 1. Class 3<br />Strings and Symbols<br />
    2. 2. Strings<br />“I am a string!”<br />
    3. 3. Quoting Strings<br />Double quotes interpolate <br />&gt;&gt; me = &quot;double quotes are awesome”<br />&gt;&gt; &quot;I am a string withsdouble quotes. #{me}&quot;<br />=&gt; &quot;I am a string with double quotes. double quotes are awesome&quot;<br />Single quotes don’t interpolate<br />&gt;&gt; &apos;I am a string withssingle quotes. #{me}&apos;<br />=&gt; &quot;I am a string withsingle quotes. #{me}&quot;<br />
    4. 4. Concatenating Strings<br />first_name = &quot;Yukihiro”<br />last_name = &quot;Matsumoto”<br />full_name = first_name + &quot; &quot; + last_name<br />=&gt; &quot;Yukihiro Matsumoto&quot;<br />full_name = first_name &lt;&lt; &quot; &quot; &lt;&lt; last_name<br />=&gt; &quot;Yukihiro Matsumoto&quot;<br />
    5. 5. String Interpolation<br />birthday = “January 5th”<br />“My birthday is #{birthday}”<br />
    6. 6. String Formatting: upcase<br />&gt;&gt; full_name.upcase<br />=&gt; &quot;YUKIHIRO MATSUMOTO”<br />&gt;&gt; full_name<br />=&gt; &quot;Yukihiro Matsumoto&quot;<br />&gt;&gt; full_name.upcase!<br />=&gt; &quot;YUKIHIRO MATSUMOTO&quot;<br />&gt;&gt; full_name<br />=&gt; &quot;YUKIHIRO MATSUMOTO&quot;<br />
    7. 7. More String Formatting<br />&gt;&gt; full_name.downcase!<br />=&gt; &quot;yukihiro matsumoto&quot;<br />&gt;&gt; full_name.capitalize<br />=&gt; &quot;Yukihiro matsumoto&quot;<br />Making our own title case method:<br />&gt;&gt; full_name.split.map {|w| w.capitalize}.join(&quot; &quot;)<br />=&gt; &quot;Yukihiro Matsumoto&quot;<br />We can also get title case using regex<br />
    8. 8. Access a String’s Characters<br />&gt;&gt; full_name[2,4]<br />=&gt; &quot;kihi&quot;<br />&gt;&gt; full_name[4..6]<br />=&gt; &quot;hir&quot;<br />
    9. 9. Ruby 1.8<br />&gt;&gt; name = &quot;Yukihiro”<br />=&gt; &quot;Yukihiro”<br />&gt;&gt; name[4]<br />=&gt; 104<br />&gt;&gt; name[4].chr<br />=&gt; &quot;h&quot;<br />&gt;&gt; name = &quot;こんいちわ”<br />=&gt;&quot;34320122334320222334320120434320124134320221”<br />&gt;&gt; name[2]<br />=&gt; 147<br />
    10. 10. Ruby 1.9<br />&gt;&gt; name = &quot;yukihiro”<br />=&gt; &quot;yukihiro”<br />&gt;&gt; name[4]<br />=&gt; &quot;h”<br />&gt;&gt; name = &quot;こんいちわ”<br />=&gt; &quot;こんいちわ”<br />&gt;&gt; name[2]<br />=&gt; &quot;い”<br />&gt;&gt; name[0]<br />=&gt; &quot;こ&quot;<br />
    11. 11. Modifying a String<br />&gt;&gt; full_name.slice!(&quot;hi&quot;)<br />=&gt; &quot;hi&quot;<br />&gt;&gt; full_name<br />=&gt; &quot;yukiro matsumoto&quot;<br />&gt;&gt; full_name[&quot;hi&quot;] = &quot;bye&quot;<br />=&gt; &quot;bye&quot;<br />&gt;&gt; full_name<br />=&gt; &quot;Yukibyero Matsumoto&quot;<br />&gt;&gt; full_name[4,2] = &quot;bye&quot;<br />=&gt; &quot;bye&quot;<br />&gt;&gt; full_name<br />=&gt; &quot;Yukibyero Matsumoto&quot;<br />
    12. 12. Query a String<br />&gt;&gt; full_name.include?(&quot;hi&quot;)<br />=&gt; true<br />&gt;&gt; full_name.empty?<br />=&gt; false<br />&gt;&gt; full_name.size<br />=&gt; 18<br />&gt;&gt; full_name.count(&quot;o&quot;)<br />=&gt; 3<br />
    13. 13. Iterate Over a String<br />each_line: process each line in a string<br />haiku = &quot;5 7 5 ”<br />haiku.each_line{|line| puts line}<br />5<br />7<br />5<br /><ul><li>each_byte: process each byte
    14. 14. each_char: process charactercareful of 1.8.x and 1.9 differences</li></ul>&gt;&gt; word = &quot;こにちわ&quot;<br />=&gt; &quot;こにちわ&quot;<br />&gt;&gt; word.each_byte do |s|<br />puts s <br />end<br />227<br />129<br />&gt;&gt; word = &quot;こにちわ&quot;<br />=&gt; &quot;こにちわ&quot;<br />&gt;&gt; word.each_char do |s| puts s <br />end<br />こ<br />に<br />ち<br />わ<br />
    15. 15. Iterate Using Split<br />returns an array of partial strings exploded at a separator<br />secret_code = &quot;the black dove flies at night”<br />secret_code.split(&quot; &quot;).each do |s| <br /> puts s.reverse<br />end<br />eht<br />kcalb<br />evod<br />seilf<br />ta<br />thgin<br />
    16. 16. Symbols<br />:i_am_a_symbol<br />
    17. 17. What is a Symbol?<br />A symbol represents a name.<br />Instances of the built-in class Symbol.<br />They efficiently describe names while saving the space one would use to generate a string for each naming instance.<br />
    18. 18. A Symbol is not a String<br /> :thing != “thing”<br />However a symbol can be create from a string:<br />“thing”.to_sym<br />And a string can be created from a symbol<br />:thing.to_s<br />
    19. 19. Symbols are Immutable<br />You can’t change a symbol<br />For example, you can’t append characters to a symbol...once a symbol exists, that’s it!<br />&gt;&gt; :name + :me<br />NoMethodError: undefined method `+&apos; for :name:Symbol<br /> from (irb):182<br /> from :0<br />&gt;&gt; :name &lt;&lt; :me<br />NoMethodError: undefined method `&lt;&lt;&apos; for :name:Symbol<br /> from (irb):183<br /> from :0<br />
    20. 20. Symbols are Unique<br />:name is the only symbol object called :name<br />&gt;&gt; :name.object_id<br />=&gt; 68828<br />&gt;&gt; :name.object_id<br />=&gt; 68828<br />“name” is a new String object each time it is instantiated<br />&gt;&gt; &quot;name&quot;.object_id<br />=&gt; 2157595700<br />&gt;&gt; &quot;name&quot;.object_id<br />=&gt; 2157591380<br />
    21. 21. Homework<br />Chapters:<br />8.1 & 8.2<br />11.1 – 11.8<br />Koans:<br />about_strings<br />Test-first-teaching<br />piglatin<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×