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›Conditions (if / else)

Conditions (if / else)

if-elsif-else, unless, ternary operator, and case/when.

Simple enough for a 5-year-old to understand

if / elsif / else

Ruby uses end for blocks (no curly braces).

Example code
1
score = 75
Value.
VariableValue.
2
if score >= 85
85+?
Condition85+?
3
puts "Excellent"
If yes.
Output / InputIf yes.
4
elsif score >= 50
Otherwise 50+?
ConditionOtherwise 50+?
5
puts "Passed"
75 falls in here.
Output / Input75 falls in here.
6
else
If none of the above.
ConditionIf none of the above.
7
puts "Failed"
8
end
Close the block.
ConditionClose the block.
VariableConditionOutput / Input
💡 Hint: Every if/def/loop closes with an end — don't forget!

About this topic

Conditional structures in Ruby are similar to other languages, but with two important differences. First is the truthiness rule: only nil and false are falsy. Zero, empty strings, and empty arrays are TRUTHY, and this is the most common trap for people coming from other languages.

The second difference is that if is an EXPRESSION: it produces a value and can be used on the right side of an assignment. That's why the ternary operator is rarely needed in Ruby; a single-line if makes the same thing more readable.

Ruby also allows putting the condition at the end of the line. This syntax reads like an English sentence in single-line conditions and is common in the Ruby community. It is a clear example of the language's priority on readability.

What you will learn

  • the if, elsif, and else structure
  • that only nil and false are falsy
  • end-of-line condition syntax
  • using unless
  • the case structure

When to use?

Case is used when a single value is compared against several possibilities; Ruby's case structure is more powerful than switch in other languages because it can also do range and class matching.

Using unless for negative conditions improves readability, but writing it together with else is confusing and should be avoided.

Common mistakes

Wrong

value = 0
if value
  puts "Value exists"
else
  puts "No value"
end

Zero is TRUTHY in Ruby; the only things considered falsy are nil and false. That's why "Value exists" is printed. People coming from Python or JavaScript get surprised here. If you want to check for zero, you need to write an explicit comparison.

Correct

value = 0
if value.zero?
  puts "Value is zero"
else
  puts "Value is not zero"
end

Wrong

score = 50
if score > 80
  puts "Successful"
elseif score > 60
  puts "Average"
end

The keyword in Ruby is ELSIF, not elseif. When elseif is written, Ruby thinks it is a method call and gives an "undefined method 'elseif'" error. This typo comes from PHP or JavaScript habits and happens very often.

Correct

score = 50
if score > 80
  puts "Successful"
elsif score > 60
  puts "Average"
else
  puts "Needs improvement"
end

Wrong

name = ""
if name
  puts "Name entered: #{name}"
end

An empty string is TRUTHY in Ruby, so the block runs and the name is printed empty. For emptiness checks, a method that asks whether the string is empty is used. To check for nil and empty strings together, there is a separate method in Rails, but in pure Ruby, you have to check both manually.

Correct

name = ""
if !name.nil? && !name.empty?
  puts "Name entered: #{name}"
else
  puts "Name is empty"
end

Frequently asked questions

When is unless used?

When the negation of a condition reads more naturally. In situations like giving a warning if something is not valid, unless reads like an English sentence. However, using unless with else creates a double negative and ruins readability.

Is Ruby's case structure different from switch?

Much more powerful. It does not only equality, but also range, class, and regular expression matching. Also, there is NO fall-through behavior to the next case; you don't need to write break. This eliminates the forgotten break bug from the C family.

When is end-of-line condition syntax appropriate?

When the condition and body are short and easily readable in a single line. When the body gets longer or the condition becomes complex, the classic block syntax is clearer. Forcing end-of-line syntax doesn't increase readability, it decreases it.

Summary

Only nil and false are falsy; zero and empty strings are considered truthy.

The keyword is elsif, not elseif.

if is an expression and produces a value.

There is no fall-through behavior in the case structure, no break is needed.

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