SlideShare a Scribd company logo
Class 3 Strings and Symbols
Strings “I am a string!”
Quoting Strings Double quotes interpolate  >> name = ”Sam” >> "hello #{name}" => "hello Sam" Single quotes don’t interpolate >> 'hello #{name}' => "hello {name}"
Concatenating Strings first_name = ”Sam” last_name = ”Smith” full_name = first_name + " " + last_name => ”Sam Smith" full_name = first_name << " " << last_name => ”Sam Smith"
String Formatting: upcase >> full_name.upcase => ”SAM SMITH” >> full_name => ”Sam Smith" >> full_name.upcase! => ”SAM SMITH" >> full_name => ”SAM SMITH"
More String Formatting >> full_name.downcase! => ”sam smith" >> full_name.capitalize => ”Sam smith" Making our own title case method: >> full_name.split.map {|w| w.capitalize}.join(" ") => ”Sam Smith"
Access a String’s Characters >> full_name[2].chr => "m” >> full_name[0,1] => "S” >> full_name[2,4] => "m Sm" >> full_name[4..6] => "Smi"
Ruby 1.8 >> name = "Yukihiro” => "Yukihiro” >> name[4] => 104 >> name[4].chr => "h" >> name = "こんいちわ” =>"43012343022343010443014143021” >> name[2] => 147
Ruby 1.9 >> name = "yukihiro” => "yukihiro” >> name[4] => "h” >> name = "こんいちわ” => "こんいちわ” >> name[2] => "い” >> name[0] => "こ"
Modifying a String >> full_name.slice!("Sam") => "Sam" >> full_name => " Smith" >> full_name["Sam"] = "Fred" => "Fred" >> full_name => "Fred Smith" >> full_name[0,4] = "Greg" => "Greg" >> full_name => "Greg Smith"
Query a String >> full_name.include?(”Greg") => true >> full_name.empty? => false >> full_name.size => 10
Iterate Over a String each_line: process each line in a string haiku = "575” haiku.each_line{|line| puts line} 5 7 5 ,[object Object],>> word = "こにちわ" => "こにちわ" >> word.each_char do |s|  puts s  end こ に ち わ
Iterate Using Split returns an array of partial strings exploded at a separator secret_code = "the black dove flies at night” secret_code.split(" ").each do |s|  	puts s.reverse end eht kcalb evod seilf ta thgin
Symbols :i_am_a_symbol
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.
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
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 + :me NoMethodError: undefined method `+' for :name:Symbol 	from (irb):182 	from :0 >> :name << :me NoMethodError: undefined method `<<' for :name:Symbol 	from (irb):183 	from :0
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

More Related Content

Viewers also liked

Ruby Class 1
Ruby Class 1Ruby Class 1
Ruby Class 1
Sarah Allen
 
Intro To Ruby
Intro To RubyIntro To Ruby
Intro To Ruby
Sarah Allen
 
Ruby File I/O
Ruby File I/ORuby File I/O
Ruby File I/O
Sarah Allen
 
Ruby Enumerable
Ruby EnumerableRuby Enumerable
Ruby Enumerable
Sarah Allen
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription Landscape
Sarah Allen
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?
Sarah Allen
 
Ruby RegEx
Ruby RegExRuby RegEx
Ruby RegEx
Sarah Allen
 
Ruby on Rails Fundamentals, Class 1
Ruby on Rails Fundamentals, Class 1Ruby on Rails Fundamentals, Class 1
Ruby on Rails Fundamentals, Class 1
Sarah Allen
 
Teaching code literacy
Teaching code literacyTeaching code literacy
Teaching code literacy
Sarah Allen
 
Test First Teaching and the path to TDD
Test First Teaching and the path to TDDTest First Teaching and the path to TDD
Test First Teaching and the path to TDD
Sarah Allen
 
Cinematic Interaction Design
Cinematic Interaction DesignCinematic Interaction Design
Cinematic Interaction Design
Sarah Allen
 

Viewers also liked (11)

Ruby Class 1
Ruby Class 1Ruby Class 1
Ruby Class 1
 
Intro To Ruby
Intro To RubyIntro To Ruby
Intro To Ruby
 
Ruby File I/O
Ruby File I/ORuby File I/O
Ruby File I/O
 
Ruby Enumerable
Ruby EnumerableRuby Enumerable
Ruby Enumerable
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription Landscape
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?
 
Ruby RegEx
Ruby RegExRuby RegEx
Ruby RegEx
 
Ruby on Rails Fundamentals, Class 1
Ruby on Rails Fundamentals, Class 1Ruby on Rails Fundamentals, Class 1
Ruby on Rails Fundamentals, Class 1
 
Teaching code literacy
Teaching code literacyTeaching code literacy
Teaching code literacy
 
Test First Teaching and the path to TDD
Test First Teaching and the path to TDDTest First Teaching and the path to TDD
Test First Teaching and the path to TDD
 
Cinematic Interaction Design
Cinematic Interaction DesignCinematic Interaction Design
Cinematic Interaction Design
 

Similar to Ruby Strings & Symbols

Lecture 6
Lecture 6Lecture 6
Lecture 6
Mohammed Saleh
 
Strings and Symbols
Strings and SymbolsStrings and Symbols
Strings and Symbols
Blazing Cloud
 
Why Scala?
Why Scala?Why Scala?
Why Scala?
Mike Fogus
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
BG Java EE Course
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
brian d foy
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex power
Max Kleiner
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Languagezone
 
Javascript
JavascriptJavascript
Javascript
guest03a6e6
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
Bernard Loire
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoobirbal
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
Raghavan Mohan
 
CGI With Object Oriented Perl
CGI With Object Oriented PerlCGI With Object Oriented Perl
CGI With Object Oriented Perl
Bunty Ray
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
Sopan Shewale
 
Lecture 10.pdf
Lecture 10.pdfLecture 10.pdf
Lecture 10.pdf
SakhilejasonMsibi
 
Prototype js
Prototype jsPrototype js
Prototype js
mussawir20
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
String functions
String functionsString functions
String functions
ssuser93a21b
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo Bergavera
 

Similar to Ruby Strings & Symbols (20)

Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Strings and Symbols
Strings and SymbolsStrings and Symbols
Strings and Symbols
 
Why Scala?
Why Scala?Why Scala?
Why Scala?
 
14 strings
14 strings14 strings
14 strings
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex power
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 
Javascript
JavascriptJavascript
Javascript
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
CGI With Object Oriented Perl
CGI With Object Oriented PerlCGI With Object Oriented Perl
CGI With Object Oriented Perl
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Javascript
JavascriptJavascript
Javascript
 
Lecture 10.pdf
Lecture 10.pdfLecture 10.pdf
Lecture 10.pdf
 
Prototype js
Prototype jsPrototype js
Prototype js
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
String functions
String functionsString functions
String functions
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 

More from Sarah Allen

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequences
Sarah Allen
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)
Sarah Allen
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical Skill
Sarah Allen
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government Services
Sarah Allen
 
Transparency Wins
Transparency WinsTransparency Wins
Transparency Wins
Sarah Allen
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of Computers
Sarah Allen
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software Fun
Sarah Allen
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of Transparency
Sarah Allen
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for Fun
Sarah Allen
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World Conference
Sarah Allen
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead People
Sarah Allen
 
Let's pretend
Let's pretendLet's pretend
Let's pretend
Sarah Allen
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby
Sarah Allen
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?
Sarah Allen
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from Toastmasters
Sarah Allen
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future Thoughts
Sarah Allen
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web Video
Sarah Allen
 
Elementary Computer History
Elementary Computer HistoryElementary Computer History
Elementary Computer History
Sarah Allen
 
Crafting Software Products
Crafting Software ProductsCrafting Software Products
Crafting Software Products
Sarah Allen
 
Agile Business Development
Agile Business DevelopmentAgile Business Development
Agile Business Development
Sarah Allen
 

More from Sarah Allen (20)

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequences
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical Skill
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government Services
 
Transparency Wins
Transparency WinsTransparency Wins
Transparency Wins
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of Computers
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software Fun
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of Transparency
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for Fun
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World Conference
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead People
 
Let's pretend
Let's pretendLet's pretend
Let's pretend
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from Toastmasters
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future Thoughts
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web Video
 
Elementary Computer History
Elementary Computer HistoryElementary Computer History
Elementary Computer History
 
Crafting Software Products
Crafting Software ProductsCrafting Software Products
Crafting Software Products
 
Agile Business Development
Agile Business DevelopmentAgile Business Development
Agile Business Development
 

Ruby Strings & Symbols

  • 1. Class 3 Strings and Symbols
  • 2. Strings “I am a string!”
  • 3. Quoting Strings Double quotes interpolate >> name = ”Sam” >> "hello #{name}" => "hello Sam" Single quotes don’t interpolate >> 'hello #{name}' => "hello {name}"
  • 4. Concatenating Strings first_name = ”Sam” last_name = ”Smith” full_name = first_name + " " + last_name => ”Sam Smith" full_name = first_name << " " << last_name => ”Sam Smith"
  • 5. String Formatting: upcase >> full_name.upcase => ”SAM SMITH” >> full_name => ”Sam Smith" >> full_name.upcase! => ”SAM SMITH" >> full_name => ”SAM SMITH"
  • 6. More String Formatting >> full_name.downcase! => ”sam smith" >> full_name.capitalize => ”Sam smith" Making our own title case method: >> full_name.split.map {|w| w.capitalize}.join(" ") => ”Sam Smith"
  • 7. Access a String’s Characters >> full_name[2].chr => "m” >> full_name[0,1] => "S” >> full_name[2,4] => "m Sm" >> full_name[4..6] => "Smi"
  • 8. Ruby 1.8 >> name = "Yukihiro” => "Yukihiro” >> name[4] => 104 >> name[4].chr => "h" >> name = "こんいちわ” =>"43012343022343010443014143021” >> name[2] => 147
  • 9. Ruby 1.9 >> name = "yukihiro” => "yukihiro” >> name[4] => "h” >> name = "こんいちわ” => "こんいちわ” >> name[2] => "い” >> name[0] => "こ"
  • 10. Modifying a String >> full_name.slice!("Sam") => "Sam" >> full_name => " Smith" >> full_name["Sam"] = "Fred" => "Fred" >> full_name => "Fred Smith" >> full_name[0,4] = "Greg" => "Greg" >> full_name => "Greg Smith"
  • 11. Query a String >> full_name.include?(”Greg") => true >> full_name.empty? => false >> full_name.size => 10
  • 12.
  • 13. Iterate Using Split returns an array of partial strings exploded at a separator secret_code = "the black dove flies at night” secret_code.split(" ").each do |s| puts s.reverse end eht kcalb evod seilf ta thgin
  • 15. 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.
  • 16. 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
  • 17. 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 + :me NoMethodError: undefined method `+' for :name:Symbol from (irb):182 from :0 >> :name << :me NoMethodError: undefined method `<<' for :name:Symbol from (irb):183 from :0
  • 18. 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

Editor's Notes

  1. &lt;&lt; is dangerous
  2. There is no title case method in ruby...we can make our own