Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
Variable scope - what is it, and how does it Ruby?

Ruby - Variable ScopeVariables 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, Dad, Graham 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:

Object and Class variablesA 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 or $current.

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
Dorothy-2:pics grahamellis$

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 [4] 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 articles
R103 - Basic Ruby Language Elements
  [4504] Where does Ruby load modules from, and how to load from current directory - (2015-06-03)
  [4369] Ruby - the second rung of learning the language - (2014-12-28)
  [4324] Learning to program - variables and constants - (2014-11-22)
  [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
  [3758] Ruby - standard operators are overloaded. Perl - they are not - (2012-06-09)
  [3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
  [3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
  [2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
  [2613] Constants in Ruby - (2010-02-01)
  [2287] Learning to program in Ruby - examples of the programming basics - (2009-07-15)
  [986] puts - opposite of chomp in Ruby - (2006-12-15)

R110 - Ruby - Special Variables and Pseudo-Variables
  [4682] One line scripts - Awk, Perl and Ruby - (2016-05-20)
  [4502] Reading and parsing a JSON object in Ruby - (2015-06-01)
  [3757] Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09)
  [2623] Object Oriented Ruby - new examples - (2010-02-03)
  [1891] Ruby to access web services - (2008-11-16)
  [1587] Some Ruby programming examples from our course - (2008-03-21)
  [1586] Variable types in Ruby - (2008-03-21)
  [990] Ruby - Totally Topical - (2006-12-16)

Back to
The dog is not in trouble
Previous and next
Horse's mouth home
Forward to
Standing on the corner, Melksham Carnival
Some other Articles
What does x on a linux directory mean?
How much space does my directory take - Linux
Melksham Carnival Parade - the people
Standing on the corner, Melksham Carnival
Variable scope - what is it, and how does it Ruby?
The dog is not in trouble
Can you learn to program in 4 days?
Regular Expressions in Ruby
Object Orientation in Ruby - intermediate examples
Collection objects (array and hash) in Ruby
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2296_Var ... Ruby-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat