When you’re learning Ruby, it’s easy to get stuck wondering what your code is actually doing, especially when you’re working with text, variables, and decision-making.
This post brings together some of the first “aha!” moments I experienced during my coding boot camp. From printing variables with interpolation, to writing clear conditionals (and nesting them), to combining strings with concatenation, these simple but powerful Ruby concepts will help you write code that is cleaner, more readable, and does what you expect. Let’s break them down one at a time.
What is concatenation in Ruby?
In Ruby, concatenation means combining two strings together. You do this using the +
operator. To put it simply, combining two strings by using the addition sign (+) is a concatenation in Ruby.
🔍 Example:
first_name = "Kelly"
last_name = "Barkhurst"
puts first_name + " " + last_name
# => Kelly Barkhurst
The +
sign joins the strings into one longer string. This usage is not math; instead it is merging characters.
Concepts to Understand
✅ The +
operator can be used to concatenate (join) strings in Ruby.
✅ It’s different from using +
with integers (math vs. string operations).
A string is text and is treated like letters. The use of the plus sign combines letters and words; in this case, the plus sign is not used for calculating numerals (integers) or solving mathematical equations.
Text sometimes contains numbers, but Ruby does not execute concatenated strings as it executes numbers being added together or solved mathematically. Instead, concatenated strings are displayed side by side.
✅ You can’t use the minus sign to remove letters (unjoin). Ruby does not allow string subtraction—trying to do "hello" - "lo"
will raise an error. It does not display hel
.
Important Notes to Recap:
- You can’t subtract strings in Ruby.
"hello" - "lo" # => Error: undefined method `-' for String
- You can’t directly add a string and a number:
"Age: " + 30 # => TypeError: no implicit conversion of Integer into String
- To combine text and numbers, you must convert the number to a string:
"Age: " + 30.to_s # => "Age: 30"
Tip: Prefer Interpolation When Possible
String concatenation works, but interpolation is usually easier to read:
puts "My name is #{first_name} #{last_name}"
# => My name is Kelly Barkhurst
What is interpolation in Ruby?
Interpolation lets you embed Ruby code inside a string. It is most commonly used to insert the value of a variable into a sentence.
To do this, you use:
#{ variable }
This #{ }
syntax tells Ruby to run the code inside the curly braces, instead of treating the code as plain text.
🔍 Example:
name = "Kelly"
puts "Hello, #{name}!" # => Hello, Kelly!
Interpolation only works in double-quoted strings (" "
), not single-quoted strings (' '
):
puts 'Hello, #{name}!' # => Hello, #{name!}
Ruby treats the single-quoted version as literal text, so no variable substitution happens.
What if my Ruby variable meets more than one condition?
In a Ruby conditional, only the first true
condition runs. This happens even if other conditions would also be true.
Ruby executes conditionals top to bottom, so the first match wins.
🔍 Example:
score = 95
if score > 90
puts "Excellent!"
elsif score > 80
puts "Great job!"
else
puts "Keep trying!"
end
Even though score > 80
is also true, only "Excellent!"
is printed because it was the first valid condition Ruby encountered.
Tip:
Use clear, non-overlapping conditions if you want to control exactly which path runs. If more than one path could match, Ruby will still only execute the first one that’s valid.
Can if
conditionals be nested in Ruby?
Yes! You can nest conditionals inside other conditionals in Ruby. Think of the conditionals like outer and inner logic blocks.
Nested conditionals are useful when one condition needs to be checked only if another condition is already true.
🔍 Example:
age = 20
student = true
if age >= 18
if student
puts "You're an adult student."
else
puts "You're an adult, but not a student."
end
end
Example Output:
If age = 20
and student = true
, you’ll see:
You're an adult student.
If student = false
, you’d see:
You're an adult, but not a student.
This example uses an if
inside another if
. Because the first age conditional is true, the logic then enters the next conditional and checks if the person is a student. Ruby supports as many levels of nesting as is needed, however deeply nested logic can quickly become hard to read.
Pro Tip: Use Indentation for Clarity
Ruby doesn’t require indentation to run, but the convention is to indent each nested block by 2 spaces. This makes your code easier to read for you and anyone else who works with it. Clean code is readable code!
Final Thoughts
Interpolation, conditionals, nesting, and string concatenation (Ruby fundamentals), are small pieces that form the foundation of writing readable, working code. They’re also common sources of confusion for beginners (I know they were for me!). If you’re just getting started, keep experimenting with these concepts in your own projects or in IRB (Ruby’s interactive console). The more you practice, the more natural they become.
If you found this helpful, check out more of my Ruby basics posts or read about why I joined a coding bootcamp to level up my development skills.
Happy coding!