Strings and Symbols

813 views

Published on

Strings and Symbols

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Strings and Symbols

  1. 1. STRINGS AND SYMBOLS Sarah Allen
  2. 2. STRINGS• “I am a string!”
  3. 3. QUOTING STRINGS• Double quotes interpolate>> me = "double quotes are awesome”>> "I am a string withsdouble quotes.n #{me}"=> "I am a string with double quotes.n double quotes are awesome"• Single quotes don’t interpolate>> I am a string withssingle quotes.n #{me}=> "I am a string withssingle quotes.n #{me}"
  4. 4. CONCATENATING STRINGSfirst_name = "Yukihiro”last_name = "Matsumoto”full_name = first_name + " " + last_name=> "Yukihiro Matsumoto"
  5. 5. STRING INTERPOLATIONbirthday = “January 5th”“My birthday is #{birthday}”
  6. 6. STRING FORMATTING: UPCASE>> full_name.upcase >> full_name.upcase!=> "YUKIHIRO MATSUMOTO” => "YUKIHIRO MATSUMOTO">> full_name >> full_name=> "Yukihiro Matsumoto" => "YUKIHIRO MATSUMOTO"
  7. 7. MORE STRING FORMATTING>> full_name.downcase!=> "yukihiro matsumoto">> full_name.capitalize=> "Yukihiro matsumoto"• Making our own title case method:>> full_name.split.map {|w| w.capitalize}.join(" ")=> "Yukihiro Matsumoto"• We can also get title case using regex
  8. 8. ACCESS A STRING’S CHARACTERS>> full_name[2,4]=> "kihi">> full_name[4..6]=> "hir"
  9. 9. RUBY 1.8>> name = "Yukihiro”=> "Yukihiro”>> name[4]=> 104>> name[4].chr=> "h">> name = " ”=>"343201223343202223343201204343201241343 20221”>> name[2]=> 147
  10. 10. RUBY 1.9>> name = "yukihiro”=> "yukihiro”>> name[4]=> "h”>> name = " ”=> " ”>> name[2]=> " ”>> name[0]=> " "
  11. 11. MODIFYING A STRING>> full_name.slice!("hi")=> "hi">> full_name=> "yukiro matsumoto">> full_name["hi"] = "bye"=> "bye">> full_name=> "Yukibyero Matsumoto">> full_name[4,2] = "bye"=> "bye">> full_name=> "Yukibyero Matsumoto"
  12. 12. QUERY A STRING>> full_name.include?("hi")=> true>> full_name.empty?=> false>> full_name.size=> 18>> full_name.count("o")=> 3
  13. 13. ITERATE OVER A STRING • each_line: process each line in a string haiku = "5n7n5n” haiku.each_line{|line| puts line} 5 7 5  each_char: process character each_byte: process each byte careful of 1.8.x and 1.9 differences>> word = " " >> word = " "=> " " => " " >> word.each_char do |s| puts>> word.each_byte do | s ends|puts send227129
  14. 14. ITERATE USING SPLIT• returns an array of partial strings exploded at a separatorsecret_code = "the black dove flies at night”secret_code.split(" ").each do |s| puts s.reverseendehtkcalbevodseilftathgin
  15. 15. MORE STRING OPERATIONSfull_name = ""=> ""full_name << first_name=> "Yukihiro"full_name << " "=> "Yukihiro "full_name << last_name=> "Yukihiro Matsumoto"
  16. 16. MORE STRING OPERATIONS"foo " * 3=> "foo foo foo "
  17. 17. SYMBOLS• :i_am_a_symbol
  18. 18. WHAT IS A SYMBOL?• A symbol represents a name.• Instances of the built-in class Symbol.• They efficiently describe names while saving the space one would use to generate a string for each naming instance.
  19. 19. A SYMBOL IS NOT A STRING :thing != “thing”• However a symbol can be create from a string: “thing”.to_sym• And a string can be created from a symbol :thing.to_s
  20. 20. SYMBOLS ARE IMMUTABLE• You can’t change a symbol• For example, you can’t append characters to a symbol...once a symbol exists, that’s it!>> :name + :meNoMethodError: undefined method `+ for :name:Symbol from (irb):182 from :0>> :name << :meNoMethodError: undefined method `<< for :name:Symbol from (irb):183 from :0
  21. 21. SYMBOLS ARE UNIQUE• :name is the only symbol object called :name>> :name.object_id=> 68828>> :name.object_id=> 68828• “name” is a new String object each time it is instantiated>> "name".object_id=> 2157595700>> "name".object_id=> 2157591380

×