DEV Community

Cover image for Ruby Basics: Variables and Scope
Jessie Rohrer
Jessie Rohrer

Posted on • Updated on

Ruby Basics: Variables and Scope

Photo by Joshua Sortino on Unsplash

If you want to try out some variable assignments on your own, I recommend using Repl.it, which is a good web based IDE.

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"
Enter fullscreen mode Exit fullscreen mode

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."
Enter fullscreen mode Exit fullscreen mode

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."
Enter fullscreen mode Exit fullscreen mode

Or do some math with your variables:

var1 = 25
var2 = 15
var1 - var2
=> 10
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)