Home‎ > ‎Ruby‎ > ‎cookbook‎ > ‎

instance variables


Overview:

Instance variables start with @
If they are unitialized they will contain "nil" data

In this example, the "class" info gets jammed into each of the "cust" variables.  When we call the "cust" variable with the ".subroutine" it runs that with the inputted data that was saved for that object. 

Code:

#!/usr/bin/ruby

class Customer
   def initialize(id, name, addr)
      @cust_id=id
      @cust_name=name
      @cust_addr=addr
   end
   def display_details()
      puts "Customer id #@cust_id"
      puts "Customer name #@cust_name"
      puts "Customer address #@cust_addr"
    end
end

# Create Objects
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

# Call Methods
cust1.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




Comments