• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Ruby Strings & Symbols
 

Ruby Strings & Symbols

on

  • 1,500 views

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

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

Statistics

Views

Total Views
1,500
Views on SlideShare
1,500
Embed Views
0

Actions

Likes
0
Downloads
9
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • << is dangerous
  • There is no title case method in ruby...we can make our own

Ruby Strings & Symbols Ruby Strings & Symbols Presentation Transcript

  • 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 = "こんいちわ”
    =>"34320122334320222334320120434320124134320221”
    >> 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 = "5 7 5 ”
    haiku.each_line{|line| puts line}
    5
    7
    5
    • each_char: process characters.Careful of 1.8.x and 1.9 differences
    >> 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