Variables and Types
Ruby is dynamically typed — no type declarations needed. Just assign a value and Ruby figures out the rest. Use snake_case for variable names. Constants start with an uppercase letter. Instance variables start with @, class variables with @@, global variables with $. Local variables are just plain names. Ruby figures out the type at runtime.
# Local variables — just assign and go
name = "Alice"
age = 30
pi = 3.14
active = true
# snake_case is the convention
first_name = "Bob"
total_count = 100
# Constants — UPPERCASE by convention
PI = 3.14159
MAX_USERS = 50
# Instance variables (used inside classes)
@name = "Alice"
@age = 30
# Class variables (shared across class)
@@total_users = 0
# Global variables (avoid these)
$global_var = "I can be accessed anywhere"
# Ruby figures out the type automatically
x = 42 # Integer
x = 3.14 # Now it's a Float
x = "hello" # Now it's a String
# No errors — Ruby is flexible like that
Try it Yourself ->