Variables have different "scopes" - in other words, a name that is allocated to a piece of computer memory and subsequently used to refer to that memory may be 'know about" to your program only within a very small area, or much more widely. It's the same IRL ("In Real Life") - consider you
and Graham Ellis
, all of which are names by which I am known, but in different scopes. You
is a very temporary variable name which can be reallocated throughout a process ... as people get on a bus, the "you" of "Where would you
like a ticket to varies every few seconds. Dad
varies from context to context, and there can be lots of dads all on that same bus. But there is probably just one Graham Ellis
there. This scoping, and the need for uniqueness, is a key element to programming ... and it's often something that newcomers to coding take a while to get their head around.
The first whiteboard photograph with this article (Fig 1 ;-) ) shows how you define the scope of your variables in Ruby. Variables that are just bare words (i.e. don't have any special character starting them) are local to the function / method in which the name is used.
That's like their family, and like referring to someone as "Bill" or "Kylie". There will be different Bills and Berts and Kylies in other families just up the road, but you just wait until Bill brings home another Kylie - the girl up the road - and you have a recipe for confusion around the dinner table. ((Does anyone still sit down for dinner??)). Variables that start with an @ character are object variables - they apply across all pieces of code that can be run on a particular object
, and variables that start with two @ characters are class variables that apply to all objects of a certain type.
I have another picture to show that:
A number of buses and trains. Each has a destinations, and a departure time and those vary ... so you are looking at a different memory location for each destination, and a single @ character. But if you ask how many pieces of transport you have defined, there's just a single answer across the class as a whole, and in Ruby you indicate that using @@ rather than @. And both of these variable type are wider ranging than local variables which apply only in a single block of code and are released when that block has finished running, rather than being retained between one method and another.
You'll here terms like "instance variables", "dynamic variables" and "object variables" used too for the @ type, and "static variables" or "class variables" used for the @@ type too, depending on who you're talking to and about which programming language - moving away from Ruby, the same concept but a different syntax and varied nomenclature is used for other languages - you'll find if you come on a Perl, PHP, Python, C, Java, Lua or Tcl course that I'll still talking about scope.
The final variables that I showed in my initial diagram start with a $. They are global variables - available anywhere through the code.
Now you might think it would be a good idea to make heavy use of these but emphatically it is NOT. It's all very well to use them to pass things around in smaller bits of code / scripts, but if more than a tiny proportion of your variables are scoped in this way, you make yourself a major maintainance problem as your program grows, and it becomes very hard to reuse the same code in several programs as the combining of useful bits of logic is likely to require a lot of patching of global variables on their way in and out of each piece of logic, and perhaps the dramatic updating of one or other piece of logic when you discover that both have used $result
There is an example that uses local, object and class variables here
(that's the example I was writing when I put these diagrams on the board during last week's Ruby Course
). Running that code you get:
Dorothy-2:pics grahamellis$ ruby d4_5
Swindon - 2 vehicles. option 1 of 4
Chippenham - 1 vehicles. option 2 of 4
Westbury - 2 vehicles. option 3 of 4
Bath - 1 vehicles. option 4 of 4
A variety of different destinations within each object (Swindon, Chippenham, Westbury and Bath) held in a variable that starts with at @ ... and a single value for the number of pieces of public transport defined  in a variable that starts @@. (written 2009-07-18, updated 2009-07-19)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesR103 - Basic Ruby Language Elements 
BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09) 
Ruby - standard operators are overloaded. Perl - they are not - (2012-06-09) 
Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10) 
Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05) 
Comparing floating point numbers - a word of caution and a solution - (2010-02-01) 
Constants in Ruby - (2010-02-01) 
Learning to program in Ruby - examples of the programming basics - (2009-07-15) 
puts - opposite of chomp in Ruby - (2006-12-15)R110 - Ruby - Special Variables and Pseudo-Variables 
Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09) 
Object Oriented Ruby - new examples - (2010-02-03) 
Ruby to access web services - (2008-11-16) 
Some Ruby programming examples from our course - (2008-03-21) 
Variable types in Ruby - (2008-03-21) 
Ruby - Totally Topical - (2006-12-16)
Some other Articles
What does x on a linux directory mean?How much space does my directory take - LinuxMelksham Carnival Parade - the peopleStanding on the corner, Melksham CarnivalVariable scope - what is it, and how does it Ruby?The dog is not in troubleCan you learn to program in 4 days?Regular Expressions in RubyObject Orientation in Ruby - intermediate examplesCollection objects (array and hash) in Ruby