class A def initialize @f = 5 end def f @f end def f= x @f = x end end a = A.new b = a c = A.new a.f = 7 x = a.f a.f = c x = a.f x.f = c class Frac public # implicit, anyone can call def initialize(num, den=1) if den == 0 raise "bogus" elsif den < 0 @num = - num @den = - den else @num = num @den = den end end def to_s if @den == 1 @num.to_s else @num.to_s + "/" + @den.to_s end end def add! r @num = (@num * r.den) + (r.num * @den) @den = @den * r.den self end def + r Frac.new(@num, @den).add! r end protected # any method in the same class can call def den @den end def num @num end def foo reduce # self.reduce # won't work end private # method can only call on itself def reduce self end end