0Pricing
Ruby Academy · Lesson

String Basics and Methods

Common string operations.

String Basics and Methods is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a String?

A String is a sequence of characters. In Ruby you create one with single or double quotes.

  • 'hello' is a single-quoted string
  • "hello" is a double-quoted string

Strings are everywhere: names, messages, file contents and more.

greeting = "Hello, Ruby!"
puts greeting

String Length

Use length (or its alias size) to count the characters in a string.

word = "banana"
puts word.length
puts word.size

Changing Case

Ruby gives you handy methods to change letter case:

  • upcase makes everything uppercase
  • downcase makes everything lowercase
  • capitalize uppercases only the first letter
name = "ruby LANG"
puts name.upcase
puts name.downcase
puts name.capitalize

Trimming Whitespace

User input often has extra spaces. Use strip to remove leading and trailing whitespace. lstrip and rstrip trim only one side.

messy = "   hello   "
puts messy.strip
puts "[" + messy.strip + "]"

Reversing a String

The reverse method returns the characters in reverse order. It is great for palindrome checks.

word = "level"
puts word.reverse
puts word == word.reverse

Accessing Characters

You can read a single character or a range using index brackets. Indexes start at 0. Negative indexes count from the end.

text = "Ruby"
puts text[0]
puts text[-1]
puts text[0, 2]

Concatenation

Join strings together with + or append in place with << (the shovel operator).

first = "Ruby"
last = "Lang"
puts first + " " + last

buffer = "Hello"
buffer << ", world"
puts buffer

Checking Content

Ask questions about a string:

  • include? checks for a substring
  • start_with? and end_with? check edges
  • empty? checks for an empty string
phrase = "learning ruby"
puts phrase.include?("ruby")
puts phrase.start_with?("learn")
puts "".empty?

Repeating Strings

Multiply a string with * to repeat it. This is handy for separators or simple drawings.

puts "=" * 10
puts "ab" * 3

Replacing Characters

The tr method translates characters one-to-one. To swap whole substrings you use gsub, covered in a later lesson.

code = "hello"
puts code.tr("l", "L")
puts code.tr("el", "ip")

Converting to Numbers

Strings that hold digits can become numbers with to_i (integer) or to_f (float). Non-numeric text becomes 0.

puts "42".to_i + 8
puts "3.14".to_f * 2
puts "abc".to_i

Quick Check

Test your understanding of basic string methods.

Recap

You learned core String tools:

  • length/size for counting
  • upcase, downcase, capitalize for case
  • strip for whitespace, reverse to flip
  • indexing, +, <<, include?, to_i/to_f

Next you will build strings from values using interpolation.

summary = "strings are fun"
puts summary.capitalize.length

Frequently asked questions

Is the “String Basics and Methods” lesson free?

Yes — the full text of “String Basics and Methods” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “String Basics and Methods”?

Common string operations. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “String Basics and Methods” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. String Basics and Methods
  2. Interpolation and Formatting
  3. Searching and Replacing
  4. Splitting and Joining
← Back to Ruby Academy