Ruby | Types of Variables

30 Jul, 2018
There are different types of variables in Ruby:
Local variables
Instance variables
Class variables
Global variables
Each variable in Ruby is declared by using a special character at the start of the variable name which is mentioned in the following table:
Local Variables: A local variable name always starts with a lowercase letter(a-z) or underscore (_). These variables are local to the code construct in which they are declared. A local variable is only accessible within the block of its initialization. Local variables are not available outside the method. There is no need to initialize the local variables.
Example:
age = 10
_Age = 20

Instance Variables: An instance variable name always starts with a @ sign. They are similar to Class variables but their values are local to specific instances of an object. Instance variables are available across methods for any specified instance or object i.e. instance variables can change from object to object. There is no need to initialize the instance variables and uninitialized instance variable always contains a nil value.
Example:
#!/usr/bin/ruby# Ruby program to illustrate # the Instance Variables  class Customer       def initialize(id, name, addr)       # Instance Variables      @cust_id = id @cust_name = name @cust_addr = addr end    # displaying result def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" endend  # Create Objectscust1 = Customer.new("1", "John", "Wisdom Apartments, Ludhiya")cust2 = Customer.new("2", "Poul", "New Empire road, Khandala")  # Call Methodscust1.display_details()cust2.display_details()
Output:
Customer id 1
Customer name John
Customer address Wisdom Apartments, Ludhiya
Customer id 2
Customer name Poul
Customer address New Empire road, Khandala
Class Variables: A class variable name always starts with @@ sign.It is available across different objects. A class variable belongs to the class and it is a characteristic of a class. They need to be initialized before use. Another way of thinking about class variables is as global variables within the context of a single class. A class variable is shared by all the descendants of the class. An uninitialized class variable will result in an error.
Example:
#!/usr/bin/ruby# Ruby program to illustrate # the Class  Variables  class Customer      # class variable @@no_of_customers = 0    def initialize(id, name, addr)       # An instance Variable @cust_id = id @cust_name = name @cust_addr = addr end  # displaying result  def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" end    def total_no_of_customers()       # class variable @@no_of_customers += 1 puts "Total number of customers: #@@no_of_customers"    endend  # Create Objectscust1 = Customer.new("1", "John", "Wisdom Apartments, Ludhiya")cust2 = Customer.new("2", "Poul", "New Empire road, Khandala")  # Call Methodscust1.display_details()cust1.total_no_of_customers()cust2.display_details()cust2.total_no_of_customers()
Output:
Customer id 1
Customer name John
Customer address Wisdom Apartments, Ludhiya
Total number of customers: 1
Customer id 2
Customer name Poul
Customer address New Empire road, Khandala
Total number of customers: 2
Global Variables: A global variable name always starts with $. Class variables are not available across classes. If you want to have a single variable, which is available across classes, you need to define a global variable. Its scope is global, means it can be accessed from anywhere in a program. By default, an uninitialized global variable has a nil value and its use can cause the programs to be cryptic and complex.
Example:
#!/usr/bin/ruby# Ruby program to illustrate # the Global  Variables  #!/usr/bin/ruby  # global variable$global_variable = 10class Class1 def print_global puts "Global variable in Class1 is #$global_variable" endendclass Class2 def print_global puts "Global variable in Class2 is #$global_variable" endendclass1obj = Class1.newclass1obj.print_globalclass2obj = Class2.newclass2obj.print_global
Output:
Global variable in Class1 is 10
Global variable in Class2 is 10
Ruby-Basics
Ruby
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.