Variables
Variables in Ruby can contain data of any type. You can use variables in your Ruby programswithout any declarations. Variable name itself denotes its scope (local, global, instance, etc.).
- A local variable (declared within an object) name consists of a lowercase letter (or an underscore) followed by name characters (sunil, _z, hit_and_run).
- An instance variable (declared within an object always "belongs to" whatever objectself refers to) name starts with an ''at'' sign (''@'') followed by aname (@sign, @_, @Counter).
- A class variable (declared within a class) name starts with two ''at'' signs (''@@'') followed by aname (@@sign, @@_, @@Counter). A class variable is shared among all objects of a class. Only one copy of a particular class variable exists for a given class. Class variables used at the top level are defined inObject and behave like global variables. Class variables are rarely used in Ruby programs.
- Global variables start with a dollar sign (''$'') followed by name characters. A global variable name can be formed using ''$-'' followed by any single character ($counter, $COUNTER, $-x). Ruby defines a number of global variables that include other punctuation characters, such as $_ and $-K.
Constants
A constant name starts with an uppercase letter followed by name characters. Class names and module names are constants, and follow the constant naming conventions. Examples: module MyMath, PI=3.1416, class MyPune.