Lesson 4 of 23 · Ruby 4.0
Build and transform text
Combine values into messages and apply simple string operations.
Learn
A String holds text. Double-quoted strings support interpolation: #{name} inserts the current value of name, and #{count + 1} inserts the value of that expression. This lets one message combine words and calculated values.
Use + to join strings. A method call such as label.upcase asks the string for an uppercase version; the dot connects a value with the method name. label.length asks for its character count. upcase returns a new string without changing label.
Single-quoted strings are useful for literal text: they do not interpolate #{...}. Quotes delimit a string in code; puts does not include those delimiters when displaying it.
Example
place = "river walk"
minutes = 25
puts "Next: #{place}, #{minutes} minutes"
puts place.upcaseExperiment
Run and compare the two messages. Edit the single-quoted message so that it also inserts the value of stop.
Run to inspect the label and its length. Change the first assignment to "east" and predict what changes. Add puts word to check whether upcase changed the original value.