Ruby Syntax
Learn the basic rules of Ruby programming
📝 Ruby Syntax Basics
Ruby syntax is designed to be natural and readable. It uses simple keywords and minimal punctuation, making code look clean and elegant. Ruby is case-sensitive and uses indentation for readability, though not required.
# Simple Ruby syntax
puts "Hello, World!"
name = "Ruby"
puts "I love #{name}!"
Output:
Hello, World! I love Ruby!
Syntax Rules
Case Sensitive
Ruby distinguishes between cases
name = "Ruby"
Name = "Python"
# Different variables!
No Semicolons
Line breaks end statements
puts "Hello"
puts "World"
# No semicolons needed
Keywords
Reserved words with special meaning
if, else, end
def, class, module
true, false, nil
Flexible
Multiple ways to write code
puts("Hello")
puts "Hello"
# Both work!
🔹 Basic Output
Ruby provides several methods to display output. The most common are puts, print, and p, each with slightly different behavior.
# puts - prints with newline
puts "Hello"
puts "World"
# print - prints without newline
print "Hello "
print "World"
# p - prints with inspect (useful for debugging)
p "Hello"
Output:
Hello World Hello World "Hello"
🔹 String Interpolation
Ruby allows you to embed variables and expressions inside strings using #{} syntax. This makes string formatting clean and readable.
# String interpolation with #{}
name = "Alice"
age = 25
puts "My name is #{name}"
puts "I am #{age} years old"
puts "Next year I'll be #{age + 1}"
# Single quotes don't interpolate
puts 'My name is #{name}' # Prints literally
Output:
My name is Alice
I am 25 years old
Next year I'll be 26
My name is #{name}
🔹 Code Blocks
Ruby uses keywords like if, def, and class to start blocks, and end to close them. Proper indentation makes code readable.
🔸 If Statement
# If-else block
age = 20
if age >= 18
puts "You are an adult"
puts "You can vote"
else
puts "You are a minor"
end
Output:
You are an adult You can vote
🔸 Method Definition
# Define a method
def greet(name)
puts "Hello, #{name}!"
puts "Welcome to Ruby"
end
# Call the method
greet("Bob")
Output:
Hello, Bob! Welcome to Ruby
🔹 Naming Conventions
Ruby follows specific naming conventions that make code consistent and readable across projects:
Variable Names:
- Local variables: lowercase with underscores (snake_case)
- Constants: UPPERCASE with underscores
- Instance variables: start with @ symbol
- Class variables: start with @@ symbols
# Naming examples
user_name = "Alice" # Local variable
MAX_SIZE = 100 # Constant
@age = 25 # Instance variable
@@count = 0 # Class variable
# Method names (snake_case)
def calculate_total
puts "Calculating..."
end
# Class names (CamelCase)
class UserAccount
# class code
end
🔹 Operators
Ruby supports standard operators for arithmetic, comparison, and logical operations:
🔸 Arithmetic Operators
# Basic math
puts 10 + 5 # Addition: 15
puts 10 - 5 # Subtraction: 5
puts 10 * 5 # Multiplication: 50
puts 10 / 5 # Division: 2
puts 10 % 3 # Modulus: 1
puts 2 ** 3 # Exponent: 8
Output:
15 5 50 2 1 8
🔸 Comparison Operators
# Comparisons
puts 5 == 5 # Equal: true
puts 5 != 3 # Not equal: true
puts 5 > 3 # Greater than: true
puts 5 < 3 # Less than: false
puts 5 >= 5 # Greater or equal: true
puts 5 <= 3 # Less or equal: false
Output:
true true true false true false
🔹 Multiple Statements
You can write multiple statements on one line using semicolons, though it's not recommended for readability:
# Multiple lines (preferred)
name = "Ruby"
version = 3.2
puts "#{name} #{version}"
# One line with semicolons (not recommended)
name = "Ruby"; version = 3.2; puts "#{name} #{version}"
Output:
Ruby 3.2 Ruby 3.2
🔹 Parentheses in Method Calls
Ruby allows you to omit parentheses in method calls, making code more natural to read:
# With parentheses
puts("Hello")
print("World")
# Without parentheses (more Ruby-like)
puts "Hello"
print "World"
# Both styles work the same
def greet(name)
"Hello, #{name}"
end
puts greet("Alice")
puts greet "Bob"
Output:
Hello WorldHello World Hello, Alice Hello, Bob