RB Ruby

Ruby: Variables & Types

What you will learn

How to declare variables in Ruby (no keywords needed), the key data types, and the fact that everything is an object.

Ruby is dynamically typed — you never declare a type, and a variable can hold a string now and an integer later. Variables don't need $, let, or var — just a name.

name = "Alice"      # String
age = 25            # Integer
height = 5.6        # Float
is_student = true   # TrueClass / FalseClass

Everything is an object

In Ruby, everything is an object — even numbers and true/false. You can call methods on anything:

-5.abs              # 5  (Integer#abs)
"hello".length     # 5  (String#length)
3.times { print "x" }  # xxx  (Integer#times)
true.class          # TrueClass
nil.class           # NilClass

Naming conventions (important in Ruby)

Convention Example Meaning
snake_case user_name Variables, methods, symbols
CamelCase UserName Classes and modules
SCREAMING_SNAKE MAX_USERS Constants
@name @name Instance variable (object attribute)
@@count @@count Class variable
$global $debug Global variable (rare, avoid)

Common mistakes

  • Trying to use camelCase for variables — Ruby convention is snake_case
  • Forgetting that Ruby is dynamically typed — a variable can change type at any time
  • Thinking true/false are special keywords — they are objects (TrueClass, FalseClass) with methods
  • Using = when you meant == in a condition — Ruby will warn you but it's easy to miss

Quick check below!