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
if / elsif / elseTernary operator / unlesscase / whentimes / upto / eachwhile
🧩 Method & Class
Ruby›REFERENCE›Condition & Loop
🔁

Condition & Loop

Decision making and repetition.

CommandDescription
if / elsif / elseExecutes code based on a condition.
Ternary operator / unlessShort if/else and inverse condition.
case / whenCompares a value against multiple options.
times / upto / eachNumeric and collection loops.
whileLoops as long as the condition is true.

if / elsif / else

Try it yourself →

Executes code based on a condition.

Syntax

if condition
  ...
elsif condition
  ...
else
  ...
end

Description

Ruby uses end for blocks (not curly braces). Indentation is not mandatory but recommended for readability.

Example

score = 75
if score >= 85
  puts "Excellent"
elsif score >= 50
  puts "Passed"
else
  puts "Failed"
end

Expected output

Passed

When to use?

Flow branching.

Common mistakes

  • ⚠️ Don't forget to write end — every if/def/loop closes with an end.

Ternary operator / unless

Try it yourself →

Short if/else and inverse condition.

Syntax

condition ? a : b

Description

? : is short if/else. unless means 'if NOT' (the opposite of if).

Example

age = 20
puts age >= 18 ? "adult" : "child"

Expected output

adult

When to use?

Short conditional value.

case / when

Try it yourself →

Compares a value against multiple options.

Syntax

case value
when x then ...
else ...
end

Description

A readable way for multi-branch control (like switch). break is not needed.

Example

day = 2
case day
when 1 then puts "Mon"
when 2 then puts "Tue"
else puts "?"
end

Expected output

Tue

When to use?

Menu, state control.

times / upto / each

Try it yourself →

Numeric and collection loops.

Syntax

n.times { |i| ... }

Description

n.times loops 0..n-1; (1..n).each iterates over a range; arr.each iterates over elements. These are preferred over for in Ruby.

Example

3.times { |i| puts i }
(1..3).each { |n| puts n * 10 }

Expected output

0
1
2
10
20
30

When to use?

Specific repetition, list traversal.

while

Try it yourself →

Loops as long as the condition is true.

Syntax

while condition
  ...
end

Description

Used when the number of repetitions is not known in advance.

Example

s = 0
while s < 3
  puts s
  s += 1
end

Expected output

0
1
2

When to use?

Indefinite repetition.

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