Ruby Basics: Variables and Scope
Tags: ruby, beginners, basics
Published: 2021-04-02 19:19:52

<em>Photo by <a href="https://unsplash.com/@sortino?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Joshua Sortino</a> on <a href="https://unsplash.com/s/photos/code?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></em>


<em>If you want to try out some variable assignments on your own, I recommend using <a href='http://repl.it'>Repl.it</a>, which is a good web based IDE.</em>

In Ruby, all variables and constants point at an object. When assigning a variable, you set the object that the variable references. Note that assignment on its own does not create a new copy of an object. Generally, when declaring variables in Ruby, convention is to use snake case, where words are separated by underscores.

**Invalid Variable Names:**
21st_street
end
jessie's_phone

A Ruby variable cannot start with a number, be a reserved Ruby word, or have punctuation/spaces.

Variables are assigned a value using the `=`, called an assignment operator. 
```
current_weather = "sunny"
```

You can interpolate a variable  in a string with the following syntax: #{var}
```
puts "The current weather for your location is #{current_weather}."
=> "The current weather for your location is sunny."
```

You can also reassign a variable:
```
current_weather = "rainy"
puts "The current weather for your location is #{current_weather}."
=> "The current weather for your location is rainy."
```

Or do some math with your variables:
```
var1 = 25
var2 = 15
var1 - var2
=> 10
```

## Local Variables
Local variables are available to the block in which they are declared. A local variable declared within a loop or method cannot be accessed outside of that loop or method. They must either begin with an underscore or lowercase letter.

#### Syntax
    var or _var
    

## Global Variables
Global variables in Ruby are accessible anywhere in the Ruby program, regardless of where they are declared. Global variable names must begin with a dollar sign ($).

The use of global variables is discouraged because they are visible anywhere in the code for a program and can be changed from anywhere in a program. This can make tracking down bugs extremely difficult. 

#### Syntax
    $var

## Class Variables
Class variables are shared by all instances of a Ruby class. If one object instance changes the value of a class variable, that new value will essentially change for all other object instances. Class variables begin with a double @ sign.

#### Syntax
    @@var

## Instance Variables
Instance variables are similar to class variables, except that their scope is to specific instances of an object. Changes to an instance variable are only available to that instance of the object.  Instance variables are declared with a single @ sign. 

#### Syntax
    @var

## Constants
Ruby constants are variables which, once assigned a value, shouldn't be changed. Ruby differs from most programming languages in that it allows developers to change constant variable values after they have been declared. The interpreter will protest with a warning, but the change will ultimately be allowed.

Constant declared outside of a class or module have global scope. If they are declared inside a class or module, they are available within the context of the class or module in which they were declared.

#### Syntax
    VAR