HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RubyVariablesData TypesOperatorsStringsArraysHash (Dictionary)Conditions (if / else)LoopsMethods (def)Classes
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Hash (Dictionary)
🔁 Condition & Loop
🧩 Method & Class
Ruby›Strings

Strings

Length, uppercase/lowercase, replacement (gsub), splitting (split), and joining (join).

Simple enough for a 5-year-old to understand

Length and case

Example code
1
puts "hello".length
Length: 7
Output / InputLength: 7
2
puts "hello".upcase
HELLO
Output / InputHELLO
Output / Input

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the word "hexlibrary" in UPPERCASE.

🎯 Expected output: HEXLIBRARY

Loading editor…

About this topic

In Ruby, strings are an instance of the String class and are MUTABLE. This is a major difference from Python and Java: in-place modification methods alter the string itself rather than creating a new object.

The consequence of mutability is that if two variables point to the same string, an in-place modification made to one will also be visible in the other. This is a classic source of unexpected bugs, and you must explicitly request a copy when needed.

Ruby's strength with strings lies in its rich set of methods. Operations like capitalizing, trimming, splitting, replacing, and reversing are done in a single line using built-in methods. Methods ending with an exclamation mark modify the string in place.

What you will learn

  • String creation and interpolation
  • The difference between single and double quotes
  • What exclamation mark methods do
  • The consequences of strings being mutable
  • Common string methods

When to use?

Interpolating values into a string is more readable than concatenation and performs automatic conversion: you don't need to manually convert numbers to strings.

Do not use in-place modifying methods on shared strings; you might inadvertently corrupt the caller's data.

Common mistakes

Wrong

text = "hello"
text.upcase
puts text

upcase RETURNS a new string and does not change the original; since the return value is not used, nothing happens and the lowercase version is printed to the screen. There is no error message. You must either use the return value or call the exclamation mark version.

Correct

text = "hello"
puts text.upcase

Wrong

def shout(text)
  text.upcase!
  text
end

message = "hello"
shout(message)
puts message

The exclamation mark method modifies the string IN PLACE, and since strings in Ruby are passed by reference, the caller's variable also gets corrupted: message is now uppercase. There is no error message, the side effect is silent. The method should work on its own copy.

Correct

def shout(text)
  text.upcase
end

message = "hello"
puts shout(message)
puts message

Wrong

count = 5
puts "Total: " + count

A string and a number cannot be concatenated; the interpreter throws a "no implicit conversion of Integer into String" error. There are two solutions: write the conversion explicitly or use string interpolation. The latter does the conversion automatically and is the idiomatic way in Ruby.

Correct

count = 5
puts "Total: #{count}"

Frequently asked questions

Why do exclamation mark methods exist?

To indicate that they modify the object in place. This is a warning sign: I am modifying the object, beware. Additionally, they return nil if no changes were made, so chaining their return values is dangerous. When in doubt, use the non-exclamation version.

What does freezing a string mean?

The freeze method makes a string immutable; any subsequent attempt to modify it will raise an error. It is useful for constant strings, and adding a frozen string comment at the top of a file automatically freezes all string literals, which improves performance.

Do Turkish characters cause problems?

Ruby uses UTF-8 by default and string methods work on a character basis, so length and slicing operations yield correct results. However, case conversion may occasionally produce unexpected results with certain Turkish letters.

Summary

Strings in Ruby are mutable and shared by reference.

String interpolation only works with double quotes.

Methods without an exclamation mark return a new string, while those with one modify it in place.

Combining a string and a number requires conversion or string interpolation.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code