Variables & Types

Ruby is a dynamic, beginner-friendly programming language, and variables are how you store and label the data your program works with — text, numbers, true/false, and "nothing".

Learn Variables & Types in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll assign variables, recognise Ruby's core types, check a value's class, and convert between strings and numbers.

What You'll Learn in This Lesson

1️⃣ Assigning Variables

You create a variable by writing a name, an = , and a value. Unlike many languages, you don't declare a type — Ruby reads the value and works out the type itself. Here are the five values you'll meet most: a String (text), an Integer (whole number), a Float (decimal), a Boolean ( true / false ), and nil (nothing).

We used .inspect on nil because plain puts nil prints a blank line — .inspect shows the value clearly as nil .

2️⃣ Types and Converting Between Them

Every value knows its own type — ask with .class . Because Ruby is dynamically typed , a variable can point to a String now and a number later. When data crosses a boundary (like user input, which always arrives as text), you convert it with .to_i (to Integer), .to_f (to Float), or .to_s (to String).

Your turn. Build a small player profile. Replace the three TODO values, keeping the right type for each, then run it.

Write a tiny miles-to-kilometres converter using a Float and interpolation. Run it with ruby convert.rb and check against the example.

📋 Quick Reference — Variables & Types

Practice quiz

Do you declare a variable's type in Ruby?

  • Yes, always
  • Only for numbers
  • No — Ruby infers the type from the value
  • Only for strings

Answer: No — Ruby infers the type from the value. Ruby is dynamically typed: you just assign a value and Ruby works out the type.

What is the class of 7?

  • Integer
  • Number
  • Float
  • Numeric

Answer: Integer. Whole numbers like 7 are of class Integer.

What is the class of 3.14?

  • Integer
  • Decimal
  • Double
  • Float

Answer: Float. Numbers with a decimal point are of class Float.

What does "10".to_i + 5 evaluate to?

  • "105"
  • 15
  • an error
  • "15"

Answer: 15. to_i converts the string "10" to the Integer 10, then 10 + 5 is 15.

What is the class of nil?

  • NilClass
  • NoClass
  • FalseClass
  • Object

Answer: NilClass. nil is the sole instance of NilClass.

Which values are 'falsy' in a Ruby condition?

  • false, nil, 0, and ""
  • Only false
  • Only false and nil
  • nil and 0

Answer: Only false and nil. Only false and nil are falsy; everything else (including 0 and "") is truthy.

What naming style do ordinary local variables use?

  • camelCase
  • snake_case
  • PascalCase
  • SCREAMING_SNAKE_CASE

Answer: snake_case. Local variables use snake_case — lowercase words joined by underscores.

A name that starts with a capital letter is treated by Ruby as a...

  • local variable
  • global variable
  • method
  • constant

Answer: constant. Ruby treats a name beginning with a capital letter as a constant.

What does a, b = 1, 2 do?

  • A syntax error
  • Parallel assignment: a becomes 1 and b becomes 2

Answer: Parallel assignment: a becomes 1 and b becomes 2. Parallel assignment sets both variables at once: a = 1 and b = 2.

What is the difference between = and == in Ruby?

  • They are the same
  • == assigns, = compares
  • = assigns a value; == compares two values
  • Both compare

Answer: = assigns a value; == compares two values. = is assignment; == tests whether two values are equal.