Thuta Learning
BasicProgrammingbeginner

Variables & Constants

Relax. We'll talk through this in plain words — no textbook voice.

A variable is basically a named box that holds a value. In Ruby, variable names are usually written in snake_case. A constant is used to store values that shouldn't change, and it starts with an uppercase letter.

ruby
first_name = "Aung"
age = 30
is_student = true

PI = 3.14159

puts first_name
puts age
puts PI

first_name, age, is_student are all variables. PI is a constant, and Ruby may give you a warning if you try to reassign it.

You should see
Aung 30 3.14159

Info

In Ruby, you don't need to declare a variable's type in advance. As soon as you assign a value, Ruby figures out the type.

Easy traps

  • A variable name can't start with a number. Write name1, not 1name.
Variables & Constants | Thuta Learning