July 2011
Intermediate to advanced
400 pages
8h 46m
English
In the previous example, I assigned values to variables such as subtotal, tax, and taxrate. Variables such as these that begin with a lowercase character are called local variables. This means they exist only within a specific part of a program—in other words, they are restricted to a well-defined scope. Here is an example:
variables.rb
localvar = "hello"
$globalvar = "goodbye"
def amethod
localvar = 10
puts( localvar )
puts( $globalvar )
end
def anotherMethod
localvar = 500
$globalvar = "bonjour"
puts( localvar )
puts( $globalvar )
endIn the previous code, there are two functions (or methods), amethod and anotherMethod, each of which is declared using the keyword def and contains code up to the keyword end. There are ...
Read now
Unlock full access