0

Explain this code

So I was going through the ruby tutorial again just to revise stuff and there was this question What is the output of this code? class A def initialize(x) puts x/2 end end class B def initialize(y) super(y+2) end end ob=B.new(6) and the answer to this question was 4. I kinda don’t understand. Can someone please explain this code line by line?

31st Dec 2017, 9:55 AM
Arfaan
Arfaan - avatar
5 Answers
+ 3
B.new(6) calls B.initialize with y=6 which calls A.initialize with (y+2)=6 which calls puts with (x/2)=4
31st Dec 2017, 10:01 AM
romangraef
+ 1
@Gordie Haha yea I forgot that sorry
31st Dec 2017, 12:38 PM
Arfaan
Arfaan - avatar
0
Oooo I get it now. Thanks!
31st Dec 2017, 11:12 AM
Arfaan
Arfaan - avatar