In Ruby, you can write multiple variables types based on 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 a same file and 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:
```markdown
display employee company w3school.io
display employee department w3school.io
Instance variable
Instance variable starts with @
and variable name.
Ruby class variable
class variable declared with @@