5 things you should understand about Ruby Variables

Despite being one of the most fundamental concepts in programming, it’s pretty hard to find a decent explanation of Ruby Variables for beginners. We’ve hand-picked a number of resources we believe cover the five thing you’ll need to understand about them.

1. Variables are used to store items you want to access later

James Bruce, of Make Use Of, provides a no-nonsense introduction to variables: Make Use of – Variables And Data Types

An example of a variable in action would be:

    the_average = (1 + 2 + 3) / 3

This “assigns” the value of (1 + 2 + 3) / 3 (which calculates to be 2) to the Variable the_average. Later in the program you might do:

    puts "the average is: #{the_average}"

This would display the text "the average is 2". Using Variables is particularly useful for:

  • Making code more readable: lots of short lines which each do a single thing are easier to understand than long ones which do many things
  • Situations where you need to use a value lots of times, in this case instead of writing (1 + 2 + 3) / 3 every time, you can just use the_average.

2. Variables are like tentacles

Well, that’s one of the six analogies that Khan Academy considers in this excellent blog post: Khan Academy - Teaching Variables: Analogies and Approaches

3. Assigning variables is simple

The Codecademy Glossary provides the simplest possible overview of assigning Variables: Codecademy - Ruby Glossary

Chris Pine, author of the excellent book Learn To Program, goes into a little bit more detail with six examples of Variables in action: Pine.fm - Variables

4. You don’t need to declare what "type" a variable is

Ruby Variables are weak-typed or dynamically-typed; meaning that you don't need to worry what types of data can be stored in them. Ruby Bacon gives a quick overview of what this all means: Ruby Bacon - Variables

5. But you do need to define where in a program it is accessible from

The most complicated aspect of Variables you need to understand is their “scope”. Technotopia gives a really clear and concise explanation of variable scope in the following article: Technotopia - Ruby Variable Scope

Ruby Monk goes one step further give some interactive examples: Ruby Monk - Variables

Other resources

If you still don’t quite understand some aspects, Zetcode and The Bastard’s Book of Ruby both provide quite comprehensive introductions to Variables.