Expand All

strings.step

goals do
  goal "Understand when to use strings"
  goal "Learn how to combine strings"
  goal "Learn about string interpolation"
end

step do
  irb <<IRB
'a string in single quotes'
"a string in double quotes"
IRB
  message 'A string is a series of characters. Strings can be created using either single or double quotes.'
  message 'These strings weren\'t saved into a variable. What happens to data not saved into a variable?'
  message 'What happens if you start a string with one kind of quote and end with another? How can you fix it?'
end

step do
  irb <<IRB
'Hello, ' + 'Jane'
'apples' * 3
IRB
  message 'Strings can be added to each other or multipled by numbers. What does this do?'
end

step do
  irb <<'IRB'
name = 'Jane'
"Hello #{name}"
IRB
  message '(the `{}` characters are generally called **curly braces**)'
  message 'This is called string interpolation. String interpolation lets you embed a Ruby statement in another string. It only works with double quotes: what happens when you try the same thing with single quotes?'
    irb <<'IRB'
"Two plus two is #{2 + 2}"
IRB
  message 'The code in the curly braces can be any valid Ruby statement. Try putting various things in the curly brackets to see what works and what doesn\'t.'
end

tip <<-'MD'
# Concatenation vs. Interpolation

    first = "Joe"
    last = "Smith"

`+` does *concatenation*

    full = first + " " + last

`#{}` does *interpolation*

    full = "#{first} #{last}"
MD

step do
  message "Try out some of these String methods."
  irb <<-IRB
'I have many characters'.length
'forwards'.reverse
'jane smith'.upcase
'a plain old sentence'.delete('aeiou')
  IRB
  message "<a href='/ruby/datatypes'>Recall</a> that you can see what methods
  are available for an object by calling `.methods` on it."
  irb "'some string'.methods"
end

explanation do
  message "Strings are a key way to present information in your programs. Since a human will probably be reading the output of your program eventually, and humans speak words rather than numbers, you will often want to use strings."
  message 'A not at all complete summary of methods on String:'
  table class: 'bordered' do
    tr do
      td 'length'
      td 'how long is this string (characters)'
    end
    tr do
      td 'reverse'
      td 'return same string, reversed'
    end
    tr do
      td 'upcase'
      td 'return same string, IN UPPER CASE'
    end
    tr do
      td 'delete([another string])'
      td 'delete all characters in the second string from the first'
    end
    tr do
      td 'methods'
      td 'get the names of all methods you can perform on string'
    end
  end
end

further_reading do
  a "Ruby's documentation for String", href: 'http://ruby-doc.org/core-2.2.0/String.html'
end

next_step 'input_and_output'