From: ES Date: 2005-05-27T09:42:35+09:00 Subject: Re: A different perspective on Ruby. Le 27/5/2005, "johng" a �crit: >|"It's 5.abs for absolute value, but Math.sqrt(5) for square root." >| >|Good point. That bugs me too. abs is a *function*, not logically a >|method. > >> The definition of abs is not questionable; it returns self, negated >> when self is negative, for all numeric value. On the other hand, >> natural sqrt() may return float or integer, or something else. That's >> the reason for sqrt() being a function in Math module. > >I may be missing something, but why does it matter what the return >type is? > >What is wrong with always returning a float just like Math.sqrt does? > >-alternatively, although I prefer the first option- > >Why couldn't the implementation of sqrt "cast" the result to an >instance >of the current self's type? So, if you ask an integer to sqrt, you get >an >integer back? > >Like I said, maybe I am missing something, but it seems that >sqrt is a verb applied to numbers. Well, essentially this is all that is required: irb(main):018:0> class Fixnum irb(main):019:1> def method_missing(sym, *args, &block) irb(main):020:2> return Math.send sym, self, *args, &block irb(main):021:2> end irb(main):022:1> end => nil irb(main):023:0> 100.sqrt => 10.0 irb(main):024:0> It is obviously not very robust but it took about eight seconds to implement. >John. E -- template void quack(duck& d) { d.quack(); }