SlideShare a Scribd company logo
STRINGS AND SYMBOLS




       Sarah Allen
STRINGS
• “I am a string!”
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}"
CONCATENATING STRINGS
first_name = "Yukihiro”

last_name = "Matsumoto”



full_name = first_name + " " + last_name

=> "Yukihiro Matsumoto"
STRING INTERPOLATION
birthday = “January 5th”



“My birthday is #{birthday}”
STRING FORMATTING:
            UPCASE
>> full_name.upcase       >> full_name.upcase!
=> "YUKIHIRO MATSUMOTO”   => "YUKIHIRO MATSUMOTO"


>> full_name              >> full_name
=> "Yukihiro Matsumoto"   => "YUKIHIRO MATSUMOTO"
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
ACCESS A STRING’S
             CHARACTERS
>> full_name[2,4]

=> "kihi"

>> full_name[4..6]

=> "hir"
RUBY 1.8
>> name = "Yukihiro”
=> "Yukihiro”
>> name[4]
=> 104
>> name[4].chr
=> "h"
>> name = "        ”

=>"343201223343202223343201204343201241343
  20221”

>> name[2]

=> 147
RUBY 1.9
>>   name = "yukihiro”
=>   "yukihiro”
>>   name[4]
=>   "h”
>>   name = "        ”
=> "          ”
>> name[2]
=> " ”
>> name[0]
=> " "
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"
QUERY A STRING
>> full_name.include?("hi")
=> true


>> full_name.empty?
=> false


>> full_name.size
=> 18


>> full_name.count("o")
=> 3
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
                                        end
s|
puts s
end
227
129
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
MORE STRING OPERATIONS
full_name = ""
=> ""
full_name << first_name
=> "Yukihiro"
full_name << " "
=> "Yukihiro "
full_name << last_name
=> "Yukihiro Matsumoto"
MORE STRING OPERATIONS

"foo " * 3

=> "foo foo foo "
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

PDF
Ruby - Uma Introdução
PDF
Big Design Conference: CSS3
PDF
Accelerated Stylesheets
PDF
Accelerated Native Mobile Development with the Ti gem
KEY
Desarrollando aplicaciones web en minutos
PPTX
Ruby Strings & Symbols
PPTX
3 Strings Symbols
PPTX
Ruby - Uma Introdução
Big Design Conference: CSS3
Accelerated Stylesheets
Accelerated Native Mobile Development with the Ti gem
Desarrollando aplicaciones web en minutos
Ruby Strings & Symbols
3 Strings Symbols

Similar to Strings and Symbols (20)

KEY
PDF
Eloquent ruby
PDF
14 ruby strings
PDF
Ruby training day1
PDF
Ruby Language - A quick tour
PPTX
Detailed description of Strings in Python
PPTX
Ruby data types and objects
PPTX
String in python lecture (3)
PPTX
Chapter 14 strings
PDF
07. Ruby String Slides - Ruby Core Teaching
PDF
Learning Ruby
PDF
Ruby 程式語言入門導覽
PPTX
Chapter 11 Strings.pptx
PDF
Kon nichi wa_ruby
PPTX
Chapter 11 Strings and methods [Autosaved].pptx
PPTX
Ruby from zero to hero
PDF
ruby1_6up
PDF
ruby1_6up
PPTX
Python Strings Basics with Common Methods.pptx
Eloquent ruby
14 ruby strings
Ruby training day1
Ruby Language - A quick tour
Detailed description of Strings in Python
Ruby data types and objects
String in python lecture (3)
Chapter 14 strings
07. Ruby String Slides - Ruby Core Teaching
Learning Ruby
Ruby 程式語言入門導覽
Chapter 11 Strings.pptx
Kon nichi wa_ruby
Chapter 11 Strings and methods [Autosaved].pptx
Ruby from zero to hero
ruby1_6up
ruby1_6up
Python Strings Basics with Common Methods.pptx
Ad

More from Blazing Cloud (20)

PDF
Rails ORM De-mystifying Active Record has_many
PDF
Active Record Introduction - 3
PDF
Rails Class Intro - 1
PDF
Your first rails app - 2
PDF
RSpec Quick Reference
PDF
Extending rails
KEY
2day Ruby Class Intro
KEY
Mobile Lean UX
KEY
Interactive Graphics
KEY
Interactive Graphics w/ Javascript, HTML5 and CSS3
KEY
Form helpers
KEY
Intro to Ruby (and RSpec)
KEY
What you don't know (yet)
KEY
Introduction to Rails
KEY
ActiveRecord
KEY
Ruby on Rails Class intro
KEY
Ruby on rails toolbox
KEY
Routes Controllers
KEY
Test Driven Development
KEY
Active Record
Rails ORM De-mystifying Active Record has_many
Active Record Introduction - 3
Rails Class Intro - 1
Your first rails app - 2
RSpec Quick Reference
Extending rails
2day Ruby Class Intro
Mobile Lean UX
Interactive Graphics
Interactive Graphics w/ Javascript, HTML5 and CSS3
Form helpers
Intro to Ruby (and RSpec)
What you don't know (yet)
Introduction to Rails
ActiveRecord
Ruby on Rails Class intro
Ruby on rails toolbox
Routes Controllers
Test Driven Development
Active Record
Ad

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Cloud computing and distributed systems.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Empathic Computing: Creating Shared Understanding
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Machine learning based COVID-19 study performance prediction
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Big Data Technologies - Introduction.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Electronic commerce courselecture one. Pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Review of recent advances in non-invasive hemoglobin estimation
Cloud computing and distributed systems.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Empathic Computing: Creating Shared Understanding
MIND Revenue Release Quarter 2 2025 Press Release
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Encapsulation_ Review paper, used for researhc scholars
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Machine learning based COVID-19 study performance prediction
20250228 LYD VKU AI Blended-Learning.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectral efficient network and resource selection model in 5G networks
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Understanding_Digital_Forensics_Presentation.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Big Data Technologies - Introduction.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Electronic commerce courselecture one. Pdf
Digital-Transformation-Roadmap-for-Companies.pptx

Strings and Symbols

  • 1. STRINGS AND SYMBOLS Sarah Allen
  • 2. STRINGS • “I am a string!”
  • 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. CONCATENATING STRINGS first_name = "Yukihiro” last_name = "Matsumoto” full_name = first_name + " " + last_name => "Yukihiro Matsumoto"
  • 5. STRING INTERPOLATION birthday = “January 5th” “My birthday is #{birthday}”
  • 6. STRING FORMATTING: UPCASE >> full_name.upcase >> full_name.upcase! => "YUKIHIRO MATSUMOTO” => "YUKIHIRO MATSUMOTO" >> full_name >> full_name => "Yukihiro Matsumoto" => "YUKIHIRO MATSUMOTO"
  • 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. ACCESS A STRING’S CHARACTERS >> full_name[2,4] => "kihi" >> full_name[4..6] => "hir"
  • 9. RUBY 1.8 >> name = "Yukihiro” => "Yukihiro” >> name[4] => 104 >> name[4].chr => "h" >> name = " ” =>"343201223343202223343201204343201241343 20221” >> name[2] => 147
  • 10. RUBY 1.9 >> name = "yukihiro” => "yukihiro” >> name[4] => "h” >> name = " ” => " ” >> name[2] => " ” >> name[0] => " "
  • 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. QUERY A STRING >> full_name.include?("hi") => true >> full_name.empty? => false >> full_name.size => 18 >> full_name.count("o") => 3
  • 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 end s| puts s end 227 129
  • 14. 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. MORE STRING OPERATIONS full_name = "" => "" full_name << first_name => "Yukihiro" full_name << " " => "Yukihiro " full_name << last_name => "Yukihiro Matsumoto"
  • 16. MORE STRING OPERATIONS "foo " * 3 => "foo foo foo "
  • 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. 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. 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
  • 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

Editor's Notes