aidy2
(aidy)
1
Hi,
The below code accesses the value of an instance variable
class Object_References
attr_reader :obj_1, :obj_2
def initialize
@obj_1 = "Object_1"
@obj_2 = "Object_2"
end
end
ref = Object_References.new
puts ref.obj_1
puts ref.obj_2
But how can I access the same value in a subclass?
class Whatever < Object_References
#how can I access the above objects variables here
end
Thanks
Aidy
Alle venerdì 16 novembre 2007, aidy ha scritto:
Hi,
The below code accesses the value of an instance variable
class Object_References
attr_reader :obj_1, :obj_2
def initialize
@obj_1 = "Object_1"
@obj_2 = "Object_2"
end
end
ref = Object_References.new
puts ref.obj_1
puts ref.obj_2
But how can I access the same value in a subclass?
class Whatever < Object_References
#how can I access the above objects variables here
end
Thanks
Aidy
You can access them as you would in the base class:
class A
def initialize
@x = 2
end
end
class B < A
def puts_x
puts @x
end
def x=(value)
@x = value
end
end
b = B.new
b.puts_x
b.x=3
b.puts_x
Output:
2
3
I hope this helps
Stefano