Ruby Basics: 5 Beginner Questions Answered Simply

R

When I first started learning Ruby during my full-stack developer Bootcamp (2022), I found myself Googling the same beginner questions over and over. If you’re just starting out, here are five common Ruby questions I had, answered clearly and simply.

👉 Curious why I enrolled in a coding Bootcamp in the first place? Read my Bootcamp story here.


1. What does break do in a Ruby loop?

The break command tells Ruby to stop a loop immediately and jump to the first line of code after the loop ends.

🔍 Example:

i = 0
while i < 10
puts i
break if i == 5
i += 1
end
puts "Loop is done!"

This loop prints numbers from 0 to 5, then exits and executes the next line puts "Loop is done!". Even though the condition i < 10 is still true, break stops the loop in its tracks.


2. What does == mean in Ruby?

The double equal sign is a comparison operator. It checks if two values are equal. This means Ruby compares their values and returns true if they match, or false if they do not match.

🔍 Example:

answer = gets.chomp
if answer == "yes"
puts "Great!"
end
  • This code examples compares the value of answer to the string "yes".
  • gets.chomp waits for the user to type something and hit Enter. Whatever they type is saved as a string in the variable answer.
  • The if line says:
    “If the value stored in answer is exactly equal to the string "yes", then run the code inside the if block.”
  • So, if the user types yes, it prints Great!.
  • If the user types anything else, nothing happens (the puts "Great!" line is skipped).
  • Don’t forget, the comparison is case-sensitive, so "yes" is not equal to "Yes" or "YES".

🔗 Want to go deeper into how Ruby operators work? Check out my post What are Ruby Operators for more syntax examples and tips.


3. What is the difference between = and == in Ruby conditionals?

Incorrectly using = and == is one of the most common mistakes made by beginner Ruby developers.

  • = is for assignment — it sets a variable: color = "blue"
  • == is for comparison — it checks if values are equal: if color == "blue"

❌ Common Mistake:

if answer = "yes"

This actually assigns "yes" to answer instead of comparing it. Ruby still runs this, but shows a warning:

warning: found = in conditional, should be ==

✅ Correct:

if answer == "yes"

Always use == in if or elsif statements.


4. What does a lowercase p mean in Ruby?

In Ruby, lowercase p is shorthand for printing with inspection and is a method that:

  • Prints the value of the object you give it.
  • Displays the “inspect” representation of the object, which means it shows it as Ruby internally represents it, including quotes around strings, array brackets, hashes, etc.
  • Automatically adds a newline after printing.

It’s mostly used for debugging because it shows more detail than puts or print.

🔍 Example:

name = "Kelly"
p name # => "Kelly"
puts name # => Kelly
  • p shows the string with quotes
  • puts shows the string without quotes

✅ Use p when:

  • You want to inspect what a variable really holds
  • You’re debugging and need quick visibility into values

Bonus:

print "Hi"   # prints without a newline
puts "Hi" # prints with a newline
p "Hi" # prints "Hi" with quotes

Newline means moving to the next line after printing — basically, adding a line break so whatever prints next appears on a new line instead of continuing right after the current output.


5. What are the rules for Ruby variable names?

Ruby has some important rules about naming variables:

✅ Allowed:

  • Letters and numbers (but can’t start with a number)
  • Underscores (_) at the beginning, middle, or end

❌ Not Allowed:

  • Hyphens (-)
  • Spaces
  • Periods (.)

🔍 Examples:

cat = 3             # ✅
tabby_cat = 5 # ✅
_cat = 7 # ✅
2cats = 10 # ❌ invalid
cat-name = 4 # ❌ invalid

🔠 Case matters in Ruby!

Ruby is case-sensitive. That means:

A = 3    # Constant
a = 6 # Variable

These are two different identifiers. Capitalized variable names are treated as constants.

➕ How assignment works:

In Ruby, assignment goes right to left. The value on the right is evaluated and stored in the variable on the left.

sum = 2 + 3   # sum is now 5

Trying to do it backward will throw a syntax error:

5 = sum   # ❌ Not valid

✅ Quick Recap

  • Variables follow strict naming rules
  • break exits loops early
  • == checks for equality
  • Use == in conditionals, not =
  • p is a debug-friendly print

💡 This post is part of a growing series of notes I wrote during my Bootcamp. You can read more about why I joined a coding Bootcamp, or explore more Ruby logic in Ruby Basics.

About the author

Kelly Barkhurst

Designer to Fullstack is my place to geek out and share tech solutions from my day-to-day as a graphic designer, programmer, and business owner (portfolio). I also write on Arts and Bricks, a parenting blog and decal shop that embraces my family’s love of Art and LEGO bricks!

By Kelly Barkhurst

Recent Posts

Archives

Categories