Ruby Strings & Symbols

1,530 views

Published on

Ruby Strings & Symbols, originally by Liah Hansen, edited by Sarah Allen

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,530
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • << is dangerous
  • There is no title case method in ruby...we can make our own
  • Ruby 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 />>> name = ”Sam”<br />>> "hello #{name}"<br />=> "hello Sam"<br />Single quotes don’t interpolate<br />>> 'hello #{name}'<br />=> "hello #{name}"<br />
    4. 4. Concatenating Strings<br />first_name = ”Sam”<br />last_name = ”Smith”<br />full_name = first_name + " " + last_name<br />=> ”Sam Smith"<br />full_name = first_name << " " << last_name<br />=> ”Sam Smith"<br />
    5. 5. String Formatting: upcase<br />>> full_name.upcase<br />=> ”SAM SMITH”<br />>> full_name<br />=> ”Sam Smith"<br />>> full_name.upcase!<br />=> ”SAM SMITH"<br />>> full_name<br />=> ”SAM SMITH"<br />
    6. 6. More String Formatting<br />>> full_name.downcase!<br />=> ”sam smith"<br />>> full_name.capitalize<br />=> ”Sam smith"<br />Making our own title case method:<br />>> full_name.split.map {|w| w.capitalize}.join(" ")<br />=> ”Sam Smith"<br />
    7. 7. Access a String’s Characters<br />>> full_name[2].chr<br />=> "m”<br />>> full_name[0,1]<br />=> "S”<br />>> full_name[2,4]<br />=> "m Sm"<br />>> full_name[4..6]<br />=> "Smi"<br />
    8. 8. Ruby 1.8<br />>> name = "Yukihiro”<br />=> "Yukihiro”<br />>> name[4]<br />=> 104<br />>> name[4].chr<br />=> "h"<br />>> name = "こんいちわ”<br />=>"34320122334320222334320120434320124134320221”<br />>> name[2]<br />=> 147<br />
    9. 9. Ruby 1.9<br />>> name = "yukihiro”<br />=> "yukihiro”<br />>> name[4]<br />=> "h”<br />>> name = "こんいちわ”<br />=> "こんいちわ”<br />>> name[2]<br />=> "い”<br />>> name[0]<br />=> "こ"<br />
    10. 10. Modifying a String<br />>> full_name.slice!("Sam")<br />=> "Sam"<br />>> full_name<br />=> " Smith"<br />>> full_name["Sam"] = "Fred"<br />=> "Fred"<br />>> full_name<br />=> "Fred Smith"<br />>> full_name[0,4] = "Greg"<br />=> "Greg"<br />>> full_name<br />=> "Greg Smith"<br />
    11. 11. Query a String<br />>> full_name.include?(”Greg")<br />=> true<br />>> full_name.empty?<br />=> false<br />>> full_name.size<br />=> 10<br />
    12. 12. Iterate Over a String<br />each_line: process each line in a string<br />haiku = "5 7 5 ”<br />haiku.each_line{|line| puts line}<br />5<br />7<br />5<br /><ul><li>each_char: process characters.Careful of 1.8.x and 1.9 differences</li></ul>>> word = "こにちわ"<br />=> "こにちわ"<br />>> word.each_char do |s| <br />puts s <br />end<br />こ<br />に<br />ち<br />わ<br />
    13. 13. Iterate Using Split<br />returns an array of partial strings exploded at a separator<br />secret_code = "the black dove flies at night”<br />secret_code.split(" ").each do |s| <br /> puts s.reverse<br />end<br />eht<br />kcalb<br />evod<br />seilf<br />ta<br />thgin<br />
    14. 14. Symbols<br />:i_am_a_symbol<br />
    15. 15. 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 />
    16. 16. 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 />
    17. 17. 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 />>> :name + :me<br />NoMethodError: undefined method `+' for :name:Symbol<br /> from (irb):182<br /> from :0<br />>> :name << :me<br />NoMethodError: undefined method `<<' for :name:Symbol<br /> from (irb):183<br /> from :0<br />
    18. 18. Symbols are Unique<br />:name is the only symbol object called :name<br />>> :name.object_id<br />=> 68828<br />>> :name.object_id<br />=> 68828<br />“name” is a new String object each time it is instantiated<br />>> "name".object_id<br />=> 2157595700<br />>> "name".object_id<br />=> 2157591380<br />

    ×