From: Marnen Laibow-Koser Date: 2009-11-20T02:48:42+09:00 Subject: Re: Re-opening an existing module and changing a method Aldric Giacomoni wrote: > "I hear and I forget; I see and I remember; I do and I understand." > Wouldn't it be nice if it were that simple - clearly they did not know > about the subtle art of debugging. > > So, I'm trying to hack at the math module. > Expected: >>> Math.sqrt(2) > => sqrt(2) > Actual result, mileage does not vary: >>> Math.sqrt(2) > => 1.4142135623731 > > require 'mathn' > module Math > alias :old_sqrt :sqrt > def sqrt x > result = old_sqrt x > if result.is_a? Float > "sqrt(#{x})" > else > result > end > end > end > > puts Math.sqrt(2) > > I had originally not attempted an alias, I just did "result = super x" > but it didn't really amount to much, either. > Where am I thinking about this wrong? Looks fine to me. Does old_sqrt actually return a Float or some other numeric type? Actually, why use type checking at all? Why not change the condition to use kind_of or simply test result.to_i - result == 0 ? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org -- Posted via http://www.ruby-forum.com/.