In Ruby, you can write multiple variable types based on the following usage.
- Global Variables
- Class Variables
- Instance variables
- Constant Variables
Global Variables in Ruby
Global variables are declared with $
.
These are available across all modules and classes in the same file and the scope is Global.
$company ="w3schools.io""
class Employee{
def display
puts "display employee company $company"
end
}
class Department{
def display
puts "display department company $company"
end
}
Output:
display employee company w3school.io
display employee department w3school.io
Instance variable
The instance variable starts with @
and variable name.
Ruby class variable
a class variable declared with @@