Question
I am looking for a more elegant way to concatenate strings in Ruby.
I currently have this line:
copy_source = "#{ROOT_DIR}/" << project << "/App.config"
Is there a cleaner or more idiomatic way to write this in Ruby?
Also, what is the difference between << and + when concatenating strings?
Short Answer
By the end of this page, you will understand the main ways to combine strings in Ruby: string interpolation, +, and <<. You will learn which approach is most idiomatic, how mutation works, when each option is appropriate, and how to avoid common mistakes when building file paths or messages.
Concept
In Ruby, string concatenation means combining smaller strings into one larger string. Ruby gives you several ways to do this, and the best choice depends on readability, performance, and whether you want to modify an existing string.
The three most common approaches are:
"Hello, " + name
"Hello, #{name}"
message = "Hello, "
message << name
Why this matters
String building appears everywhere in real programs:
- generating file paths
- formatting log messages
- building URLs
- producing user-facing text
- assembling SQL snippets or commands carefully
In Ruby, these methods are not identical:
+returns a new string<<appends to the existing string and mutates it- interpolation (
#{...}) is usually the most readable when inserting values into a string literal
For your example, the most idiomatic Ruby version is often:
copy_source = "#{}//App.config"
Mental Model
Think of a string like a piece of paper with text on it.
+is like taking two papers and making a new combined copy.<<is like writing extra text directly onto the same paper.- interpolation is like filling in blanks in a prepared sentence template.
For example:
"Hello, " + name→ make a new papergreeting << name→ modify the original paper"Hello, #{name}"→ use a template with a placeholder
This difference matters because modifying the original object can affect other variables that point to the same string.
Syntax and Examples
Core syntax
1. String interpolation
name = "Ava"
message = "Hello, #{name}!"
Interpolation is usually the clearest choice when you are inserting values into a string.
2. Using +
name = "Ava"
message = "Hello, " + name + "!"
This creates a new string each time + is used.
3. Using <<
name = "Ava"
message = "Hello, "
message << name << "!"
This appends to message directly.
Your example
A cleaner version using interpolation:
copy_source = "#{ROOT_DIR}/#{project}/App.config"
A better version for file paths:
Step by Step Execution
Consider this code:
root_dir = "/projects"
project = "alpha"
copy_source = "#{root_dir}/" << project << "/App.config"
Step by step
-
Ruby evaluates
"#{root_dir}/"root_diris"/projects"- result becomes
"/projects/"
-
Ruby applies
<< project- appends
"alpha"to the existing string - string becomes
"/projects/alpha"
- appends
-
Ruby applies
<< "/App.config"- appends
"/App.config" - final result is
"/projects/alpha/App.config"
- appends
So the final value is:
"/projects/alpha/App.config"
Real World Use Cases
Common places string concatenation is used
File paths
File.join("/var", "log", "app.log")
Used when locating config files, uploads, or templates.
Log messages
puts "User #{user_id} logged in at #{Time.now}"
Interpolation is very readable for logs.
URLs
base_url = "https://api.example.com"
endpoint = "users"
url = "#{base_url}/#{endpoint}"
Useful when building API requests.
Report generation
line = "Name: #{user.name}, Email: #{user.email}"
Good for CSV-like output or plain text exports.
Building large strings efficiently
output = ""
items.each do ||
output << item.to_s <<
Real Codebase Usage
In real Ruby projects, developers usually choose the string-building style based on intent.
Common patterns
Prefer interpolation for readability
error_message = "Invalid email: #{email}"
This is usually easier to read than multiple + operations.
Use File.join for paths
config_path = File.join(ROOT_DIR, project, "App.config")
This is the standard approach in codebases because it is less error-prone.
Use << when building strings repeatedly
buffer = ""
records.each do |record|
buffer << record.name << "," << record.id.to_s << "\n"
end
This avoids creating many temporary strings.
Guard against nil
name = user.name ||
message =
Common Mistakes
1. Using << without realizing it mutates the string
a = "Hello"
b = a
b << " world"
puts a
# => "Hello world"
Why this happens:
aandbrefer to the same string object<<changes that object
How to avoid it:
a = "Hello"
b = a.dup
b << " world"
2. Using + with nil
Broken code:
name = nil
message = "Hello, " + name
This raises an error because nil is not a string.
Safer options:
message = "Hello, #{name}"
or
Comparisons
| Approach | Mutates existing string? | Returns new string? | Best use case |
|---|---|---|---|
Interpolation ("#{value}") | No | Yes | Readable string templates |
+ | No | Yes | Simple one-off concatenation |
<< | Yes | No | Appending efficiently, especially in loops |
File.join | No | Yes | Building file paths |
Interpolation vs +
"Hello, "
+ name
Cheat Sheet
# Interpolation
"Hello, #{name}"
# Concatenation with +
"Hello, " + name
# Append with <<
message = "Hello, "
message << name
Quick rules
+creates a new string<<modifies the existing string- interpolation is usually the most readable for templates
- use
File.joinfor file paths - use
<<when building a string repeatedly in a loop
Good examples
full_name = "#{first} #{last}"
path = File.join(ROOT_DIR, project, "App.config")
Watch out for
"Hello, " + nil # error
Safer:
"Hello, #{nil}"
+ .to_s
FAQ
What is the most idiomatic way to concatenate strings in Ruby?
Usually string interpolation, such as "#{name}", is the most idiomatic when building readable strings. For file paths, prefer File.join.
What is the difference between << and + in Ruby?
+ returns a new string. << appends to the existing string and changes it.
Is << faster than + in Ruby?
Often yes when repeatedly building strings, because it avoids creating as many temporary string objects.
Should I use interpolation for file paths?
It works, but File.join is usually better because it is clearer and safer for path construction.
Can << cause bugs?
Yes. If two variables reference the same string, appending with << changes that shared object.
Does interpolation work with numbers and other values?
Yes. Ruby will convert the value to a string inside #{...}.
Why does "text" + nil fail?
Mini Project
Description
Build a small Ruby script that generates file paths for multiple projects. This demonstrates readable string construction and shows why File.join is often better than manual concatenation when working with file system paths.
Goal
Create a Ruby program that builds config file paths for several project names and prints them.
Requirements
Requirement 1 Requirement 2 Requirement 3
Keep learning
Related questions
Calling a Class Method from an Instance in Ruby
Learn how to call a class method from an instance in Ruby using self.class, with examples, pitfalls, and practical usage patterns.
Calling an Overridden Monkey-Patched Method in Ruby
Learn how to call the original method when monkey patching in Ruby, including alias_method patterns, examples, pitfalls, and practical usage.
Convert a Unix Timestamp to Ruby DateTime
Learn how to convert Unix timestamps to Ruby DateTime and Time objects, with examples, differences, pitfalls, and practical Ruby usage.