Variables and Assignment

Variables are the memory locations which hold any data to be used by any program. There are five types of variables supported by Ruby.

  1. Ruby Global Variables.
  2. Ruby Instance Variables.
  3. Ruby Class Variables.
  4. Ruby Local Variables.
  5. Ruby Pseudo-Variables.

Now, we will learn about all these variables one by one.

Ruby Global Variables

Global variables begin with $. Uninitialized global variables have the value nil  in Ruby which is equivalent to NULL, more specifically "In Ruby nil is an object, or more exactly "The class of the singleton object nil.",  in other programming.and produce warnings with the -w option.

Assignment to global variables alters global status. It is not recommended to use global variables. They make programs cryptic. 

Syntax :
$globalVariable = 10

Ruby Instance Variables

Instance variables begin with @. Uninitialized instance variables have the value nil and produce warnings with the -w option.

@username = 'w3clan'

Ruby Class Variables

Class variables begin with @@ and must be initialized before they can be used in method definitions.  Referencing an uninitialized class variable produces an error. Class variables are shared among descendants of the class or module in which the class variables are defined. Overriding class variables produce warnings with the -w option.

class Customer

   @@users = 0

    def total_no_of_customers()
       puts "Total number of customers: #@@users"
    end

end

Ruby Local Variables

Local variables begin with a lowercase letter or  _.  The scope of a local variable ranges from class, module, def, or do to the corresponding end or from a block's opening brace to its close brace {} .   When an uninitialized local variable is referenced, it is interpreted as a call to a method that has no arguments.  

Assignment to uninitialized local variables also serves as variable declaration. The variables start to exist until the end of the current scope is reached. The lifetime of local variables is determined when Ruby parses the program.

class Customer

   def initialize(id, name, email)
      @userid=id
      @username=name
      @useremail=email
   end

end

In above example, idname email are considered as local variable.

Ruby Constants

Constants begin with an uppercase letter. Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally.

Constants may not be defined within methods. Referencing an uninitialized constant produces an error. Making an assignment to a constant that is already initialized produces a warning.

class Examination

   Marks1 = 100
   Marks2 = 90

   def show

       puts "Marks of User Marks1 is #{VAR1}"
       puts "Marks of User Marks2 is #{VAR2}"

   end

end

Ruby Pseudo-Variables

They are special variables that have the appearance of local variables but behave like constants. You can not assign any value to these variables.

  • self: The receiver object of the current method.
  • nil: Value representing undefined.
  • __FILE__: The name of the current source file.
  • __LINE__: The current line number in the source file.

Important Note : To Print out any variable mentioned above, all you need to do is, simply put # before the variable like :-  #@variable ,  #variable , #@@variable


Loading ...

Related Results :

  1. Variables and Assignment
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author