[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Why is there a seperate Math class?

Peña, Botp

1/9/2007 3:32:00 AM

Daniel Finnie [mailto:danfinnie@optonline.net] :
# >> (Math.methods - Object.methods).select {|x|
# Math.method(x).arity ==
# 1}.each do |x|
# ?> Numeric.class_eval { ?> define_method(x.to_sym) {
# Math.send(x.to_sym, self) } >> }
# >> end
# => ["tan", "frexp", "sinh", "exp", "acos", "tanh", "log", "asin",
# "acosh", "cos", "log10", "atan", "erf", "asinh", "sin",
# "sqrt", "cosh",
# "erfc", "atanh"]
# >> *5.sqrt*
# => 2.23606797749979
# >> *Math.sqrt(5)*
# => 2.23606797749979
# >>
#
# I think the first one, 5.sqrt, looks much better than Math.sqrt(5).

Indeed, but only on the first glance.

Math.sqrt *is the sine function; which in turn has its own set of properties/behavior among the gazillion of other functions found in mathematics. You pass only 5 here as the parameter. So it is not really object for objects sake. We have to bear in mind what object we want to center our minds on. Here, in this case, the object may be the sine behavior, and the behavior varies by changing the parameter provided. And hey, is 5 in degrees or in radians? Maybe that should be 5.radians.sine or sine(5.radians) --just joking :)

kind regards -botp

ps: i do also use 5.sin though. sometimes, i feel it is not good to stick to traditions especially when i'm teaching my kids ruby. in programming, i do not want them to think that *it's the *only way (we write things).. Welcome to the brave new world of ruby.

# Thanks,
# Dan