Ruby example
## Ruby example for John
# Initializing instance variable on superclass.
# class B is a subclass of A and inherits attribute a from superclass.
# Then in B we add attribute b.
class A
attr :a, true
end
class B < A
attr :b, true
def initialize(a,b)
@a = a
@b = b
end
end
a = A.new
a.a = "value a for object a"
b = B.new("value a for object b", "value b for object a")
puts b.a
#b.b = "b"
puts "b.b is " + b.b
puts "b.a is " + b.a
puts "a.a is " + a.a
=begin
PRINTOUT WILL BE
value a for object b
b.b is value b for object a
b.a is value a for object b
a.a is value a for object a
=end
blog comments powered by
Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment