Variables

Contents
Assignment
Naming rules snake_case
Parallel assignment
Constants shout

Assignment

A variable stores a value under a name so you can reuse it later. The single equals sign copies the value on the right into the name on the left.

name = "Ada" age = 36 puts name # prints Ada puts age # prints 36

Variables hold any object, and you may store a new value in the same name at any time. Ruby never asks you to declare a type first.

Naming rules snake_case

Local variable names start with a lowercase letter or an underscore, then use lowercase letters, digits, and underscores. Words are joined with underscores, a style called snake_case.

Names cannot start with a digit and cannot contain spaces or dashes. A leading capital letter would turn the name into a constant, which is the topic of the last section.

first_name = "Ada" score_2 = 10 puts first_name # prints Ada

Parallel assignment

Ruby can fill several variables in one line when the counts on both sides match. The first value goes into the first name, the second into the second name, and so on.

x, y = 1, 2 puts x # prints 1 puts y # prints 2

The classic trick is a swap with no helper variable. The right side builds a pair first, then Ruby unpacks it into the two names:

x, y = y, x puts x # prints 2 puts y # prints 1

Constants shout

A constant holds a value that should stay fixed, such as a limit or a title. Constants start with a capital letter, and multi-word constants use all caps with underscores.

LIMIT = 10 APP_NAME = "Demo" puts LIMIT # prints 10

Ruby only warns when you reassign a constant instead of stopping you, so treat the warning as an error in your own code and pick a new name when a value must change.

Next: Numbers

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: