Posts Tagged variables
Variable vs Objects
person = “Tim”
p person.object_id
p person.class
p person
#~ 22901710
#~ String
#~ “Tim”
- Ruby creates a new String object with the value “Tim”
- A reference to this object is placed in the local variable person.
- The o/p revels this variable(person) has indeed taken on the personality of a string, with an object id, a type, and a value.
So, is a variable an object?
NO.
- A variable is simply a reference to an object.
- Objects float around in a big pool somewhere (the heap, most of the time) and are pointed to by variables.
person1 = “Tim”
person2 = person1
person2[0] = “J”
p person1.object_id
p person2.object_id
p person1
p person2
#22899620
#22899620
#”Jim”
#”Jim”
variables hold references to objects, not the objects themselves.
- person2 = person1 :: doesn’t create any new objects;
- it simply copies person1’s object reference to person2, so that both
person1 and person2 refer to the same object.
So = symbol here works as an aliasing object >> Gives you multiple variables
And all these variables reference to the same object.
Then how can we create a duplicate object instead a reference variable ?
Using dup method:
person1 = “Tim”
person2 = person1.dup
person1[0] = “J”
p person1.object_id
p person2.object_id
p person1
p person2
#~ 22901200
#~ 22901190
#~ “Jim”
#~ “Tim”
But what if you want to prevent from any modifications to the object?
person1 = “Tim”
person2 = person1
person1.freeze # prevent modifications to the object
person2[0] = “J” # can’t modify frozen string (TypeError)
Add comment March 22, 2008
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
