Strings

View saved

Create strings

Single and double quotes both work. Double quotes interpolate.

name = "Ada"
puts 'Hello #{name}'
puts "Hello #{name}"

Common methods

Strings are mutable in Ruby unless frozen.

text = "  ruby  "
puts text.strip
puts text.upcase
puts "ruby".include?("ub")

Combine and format

Interpolation keeps most string building readable.

count = 3
puts "Completed #{count} lessons"

String habits

  • Prefer interpolation over long concatenation chains
  • Symbols (next topics) are different from strings
  • Use gets for simple CLI input and chomp the newline
  • Encoding rarely matters early, but UTF-8 is the default today

Comments

One comment per signed-in account. Comments are saved with this page’s URL.