From: Robert Klemme Date: 2005-05-27T17:50:19+09:00 Subject: Re: A different perspective on Ruby. Glenn Parker wrote: > Brian Schr�der wrote: >> On 26/05/05, gsinclair@gmail.com wrote: >>> ES wrote: >>> >>>> http://www.ericw.org/rants/showrant.psp?rant=ruby >>> >>> "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. >> >> Why not 5.sqrt instead. +1 > Hmmm, prefix, postfix, or infix? Functional or object-oriented? > These are bottomless questions. > > One justification for sqrt(5) would be that "sqrt", works on more than > one class of objects (Fixnum, Bignum, Rational, etc). So does "abs" > for that matter, and I'm not crazy about 5.abs. As a method, sqrt > would have to be defined in every applicable class, either with new > code or as an included module. This spreads the definition out, > which complicates maintainence. This is not true, for two reasons: First, there is indeed a place to put functions that have the same implementation for all numeric types - so it does not have to spread: >> Fixnum.ancestors => [Fixnum, Integer, Precision, Numeric, Comparable, Object, Kernel] >> Bignum.ancestors => [Bignum, Integer, Precision, Numeric, Comparable, Object, Kernel] >> Float.ancestors => [Float, Precision, Numeric, Comparable, Object, Kernel] >> ObjectSpace.each_object(Class){|cl| p cl if cl.ancestors.include? Numeric} Bignum Float Fixnum Integer Numeric Second, it's most likely that you actually *want* different implementations for performance and optimization reasons (see the recently discovered bug with exponentiation). > Another reason is that the prefix style, sqrt(5), is somewhat closer > to the typical mathematical notation. I can't think of any unary > mathematical operations that are naturally postfix, unless you count > C++'s post-decrement and post-increment. Well, this can remain in place as a simple forward to not break compatibility def Math.sqrt(n) n.sqrt end > This is all just criticism of library design, which is very much a > matter of style and taste. The "standard" library could stand some > attention to smooth out these style bumps, possibly by allowing either > prefix or postfix styles whenever possible. Then questions of style > would be forwarded to the programmer. True. Kind regards robert