R
R. Kumar
Note: this is *not* a ruby bug. its a general query.
Perhaps best explained with sample code.
#!/usr/bin/env ruby -w
class Parent
def printa arg
puts "inside parent printa #{arg} "
printb arg
end
def printb arg
puts "inside parent printb #{arg} "
puts "--> #{arg} "
end
end
class Child < Parent
def printa arg
puts "inside child printa #{arg} reduced 1 "
# 1 is actually some other instance level variable
super arg-1
end
def printb arg
puts "inside child printb #{arg} reduced 1"
super arg-1
end
end
if __FILE__ == $0
begin
p = Parent.new
c = Child.new
puts " parent calls with 5 and 6"
p.printa 5
#p.printb 6
puts " === child calls with 7 === "
c.printa 7
#c.printb 7
ensure
end
end
Class Child extends Parent. It extends 2 methods by modifying the
incoming parameter and then calling super. One of these methods printa
calls printb.
When Parent's printa is called, it calls it own printb.
However, when Child's printa is called, it calls Parent's printa, which
(in this case) i was hoping would call Parent's printb directly. But it
(correctly) calls Child's printa which once again reduces the arg.
So my arg gets reduced twice. I suppose i could use some flags to
prevent this, But is there any direct way i can coerce Parent printa to
call only Parent printb.
When you execute the above code, you will see that 7 gets decremented 2
times.
Attachments:
http://www.ruby-forum.com/attachment/4448/test.rb
Perhaps best explained with sample code.
#!/usr/bin/env ruby -w
class Parent
def printa arg
puts "inside parent printa #{arg} "
printb arg
end
def printb arg
puts "inside parent printb #{arg} "
puts "--> #{arg} "
end
end
class Child < Parent
def printa arg
puts "inside child printa #{arg} reduced 1 "
# 1 is actually some other instance level variable
super arg-1
end
def printb arg
puts "inside child printb #{arg} reduced 1"
super arg-1
end
end
if __FILE__ == $0
begin
p = Parent.new
c = Child.new
puts " parent calls with 5 and 6"
p.printa 5
#p.printb 6
puts " === child calls with 7 === "
c.printa 7
#c.printb 7
ensure
end
end
Class Child extends Parent. It extends 2 methods by modifying the
incoming parameter and then calling super. One of these methods printa
calls printb.
When Parent's printa is called, it calls it own printb.
However, when Child's printa is called, it calls Parent's printa, which
(in this case) i was hoping would call Parent's printb directly. But it
(correctly) calls Child's printa which once again reduces the arg.
So my arg gets reduced twice. I suppose i could use some flags to
prevent this, But is there any direct way i can coerce Parent printa to
call only Parent printb.
When you execute the above code, you will see that 7 gets decremented 2
times.
Attachments:
http://www.ruby-forum.com/attachment/4448/test.rb