Contents

Roo

Variables

Variables must be declared before they are used. This is done with the var keyword. A variable may be assigned a value when declared. If a value is not assigned at declaration time then it defaults to Nothing.

var age # => Nothing.
var name = "Spider-Man"

Scope

A scope is a region where a name maps to a certain entity. Multiple scopes enable the same name to refer to different things in different contexts. Variables declared at the top level of the script are global in scope. That is, they are visible to the whole program. Variables declared in functions, classes and modules are scoped to the containing function, class or module. They are encapsulated within their enclosing scope.

Thanks to encapsulation, it is possible to shadow a variable in an enclosing scope with a newly declared variable in an inner scope:

# How loud?
var volume = 11

# Silence.
volume = 0

# Calculate size of 3x4x5 cuboid.
{
  var volume = 3 * 4 * 5
  print(volume) # => 60
}

print(volume) # => 0

Re-declaring a variable in the same scope will overwrite it without raising an error:

var team = "Manchester United"
var team = "Chelsea"
print(team) # => "Chelsea"