Posts Tagged instance
variables in Ruby
-Ruby has three kinds of variables:
- Global variables
- Instance variables
- Local variable
-Constant
e.g GVAL = “9.8′
-And two pseudo-variables.
- self
- nil
self, which always refers to the currently executing object, and nil, which is the meaningless value assigned to uninitialized variables. Both are named as if they are local variables, but self is a global variable maintained by the interpreter, and nil is really a constant.
<<<<<<<<<<< GLOBAL VARIABLES >>>>>>>>>>>
1. A global variable has a name beginning with $.
2. It can be referred to from anywhere in a program.
3. Before initialization, a global variable has the special value nil.
4. Prayas ke style me bole to :These are prasadam(God’s) & can be shared by anybody.
class Myclass
$global_var = 99;
def method1
p "In method1"
p "Global var = #{$global_var}"
end
def method2
$global_var += 1;
p "In method2"
p "Modified global var = #{$global_var}"
end
p "Inside the class global var = #{$global_var}"
end
o1 = Myclass.new
o1.method1
o1.method2
p "Outside the class #{$global_var}"
o/p:
"Inside the class global var = 99"
"In method1"
"Global var = 99"
<<<<<<<<<<< INSTANCE VARIABLES >>>>>>>>>>>
1. Starts with @. Instance variables is bounded to an object. So as many methods are associated with that object, are gonna share a single copy of it.
2. Means any change will be reflected to remainins methods.But when you call a method with a new object (o2) it re-initializes the value of instance variables.
3. Prays ke style me bole to : I say these are PRIVATE to object not to method. [same family(Indian) can share a meal in a single plate; but they feel shy to offer to a nonmember of that family ]
class Myclass
def method1
@instance_var = 99;
p "In method1"
p "Instance var = #{@instance_var}"
end
def method2
@instance_var += 1;
p "In method2"
p "Instance var = #{@instance_var}"
end
def method3(parameter1)
p "Instance var without declaration, directly use it."
p "And value is @para1 = #{parameter1}"
end
end
o1 = Myclass.new
o1.method1
o1.method2
o2 = Myclass.new
o2.method1
o2.method3(999)
o/p::
"In method1"
"Instance var = 99"
"In method2"
"Instance var = 100"
"In method1"
"Instance var = 99"
"Instance var without declaration, directly use it."
"And value is @para1 = 999"
Q. Can a metod access an instance varibale used in other method?
A. Yes provided for the same object call. [@instance_var in method1 can be accessed in method2]
Q. Is instance variables are same like local variable?
A. NO, local varibales are diff and they do exist in ruby.
<<<<<<<<<<< LOCAL VARIABLES >>>>>>>>>>>
1. starts with no $ or no @, yes they must start with eith lower case letter or an underscore (_mylocalvar)
2. must initialize befor you use else error
3. Prayas ke style me bole to: These are private to methods (Chats of bed room cant be shared in dining room). Generally, the scope of a local variable is one of
* proc{ … }
* loop{ … }
* def … end
* class … end
* module … end
* the entire script (unless one of the above applies)
class Myclass
def method1
local_var = 99;
p "In method1"
p "Local var = #{local_var}"
p defined?(local_var)
end
def method2
# local_var += 21; You must initialize b4 it is used.
local_var_other = 55
p "In method2"
p "Local var = #{local_var_other}"
p defined?(local_var)
p defined?(local_var_other)
end
end
o1 = Myclass.new
o1.method1
o/p::
"In method1"
"Local var = 99"
"local-variable"
"In method2"
"Local var = 55"
nil
"local-variable"
Add comment March 22, 2008
Instance var Vs Class var
# class varibale starts with @@
# Must be initialized before they are used
# A class variable is shared among all objects of a class
# There is only one copy of a particular class variable for a given class
# It is also accessible to the class methods
#Sometimes a class needs to provide methods that work without being tied to any particular object.
# For example the “new” method creates a new object but is not itself associated with a particular application.
# And you are calling them as App.new() rather than calling as obj1.new() or obj2.new()
#Hence “new” will be said as a class method not an instance method.
#class methods sprinkled throughout the Ruby librariesclass methods sprinkled throughout the Ruby libraries. other eg File.delete associated with File class
# $super_count – global variable – Can access anybody
# @@count – class varibles – Change will be reflected to all
# @call – instance variobles – Change will be reflected in only concern method
# callme – instance method -
# new – class method
#Class methods are defined by placing the class name and a period in front of the method name.
class App
$super_count = 111
@@count = 0
def initialize (caller)
@call = caller
end
# instance method
def callme
p “called using #{@call}”
@@count+=1
end
# class method
def App.callmetoo
p “called using callmetoo”
@@count+=1
end
end
obj1 = App.new(”OBJECT1?)
obj2 = App.new(”OBJECT2?)
p obj1.callme
p obj2.callme
p App.callmetoo
p $super_count #can be access outside the class
#p App.@@count #cant access outside the class
OUTPUT
“called using OBJECT1?
1
“called using OBJECT2?
2
“called using callmetoo”
3
111
Add comment March 22, 2008