[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Why is there a seperate Math class?

Daniel Finnie

1/9/2007 1:34:00 AM

Why does everything have to be Math.<func>(num)? Isn't num.func more
object oriented-ish?

For example:
>> (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).

Thanks,
Dan

18 Answers

Bil Kleb

1/9/2007 1:51:00 AM

0

Daniel Finnie wrote:
> Why does everything have to be Math.<func>(num)? Isn't num.func more
> object oriented-ish?

That was one the first questions I had
when I came to the Ruby world.

FWIW, here's a related answer from Matz,

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-...

Regards,
--
Bil Kleb
http://fun3d.lar...

Daniel Finnie

1/9/2007 2:21:00 AM

0

I have to think this is the first time I've ever really disagreed with
matz and don't really understand his logic.

If I were to ask someone for the length of the word apples, I would say
"What is the length of apples?" In ruby, this would be "apples".length.

If I were to ask someone for the sine of, say, 90, I would say "What is
the sine of 90?" In ruby, this would be Math.sin(90) even though the
structure of the sentence is the same as for "apples" above.

Dan

Bil Kleb wrote:
> Daniel Finnie wrote:
>> Why does everything have to be Math.<func>(num)? Isn't num.func more
>> object oriented-ish?
>
> That was one the first questions I had
> when I came to the Ruby world.
>
> FWIW, here's a related answer from Matz,
>
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-...
>
> Regards,
> --
> Bil Kleb
> http://fun3d.lar...
>
>

Gregory Brown

1/9/2007 2:34:00 AM

0

On 1/8/07, Daniel Finnie <danfinnie@optonline.net> wrote:
> I have to think this is the first time I've ever really disagreed with
> matz and don't really understand his logic.
>
> If I were to ask someone for the length of the word apples, I would say
> "What is the length of apples?" In ruby, this would be "apples".length.
>
> If I were to ask someone for the sine of, say, 90, I would say "What is
> the sine of 90?" In ruby, this would be Math.sin(90) even though the
> structure of the sentence is the same as for "apples" above.

but 90.sin * 10 looks as ugly as can be.

dblack

1/9/2007 3:04:00 AM

0

Gavin Kistner

1/9/2007 6:34:00 AM

0

Daniel Finnie wrote:
> Why does everything have to be Math.<func>(num)? Isn't num.func more
> object oriented-ish?

In addition to the answers by others, what would you do for those math
functions whose arity isn't 1?

Math.methods(false).select{ |n| Math.method(n).arity != 1 }
#=> ["hypot", "atan2", "ldexp"]

All three take two arguments. While 3.hypot(4) *might* make
sense--given one leg of a right triangle, how long is the hypotenuse to
a given other leg--30.atan2( 40 ) makes little sense to me.

A few exceptions might not make a good arguments against 19 possibly
plausible cases. In my opinion, however, once you have to break the
rule it makes sense to look for a new way to handle all the cases
similarly.

And, personally, I agree with Matz - the trigonometric functions simply
look more correct (to me) as functions that take an argument, not
methods invoked upon a number. That's purely a matter of taste, though.

Erik Veenstra

1/9/2007 8:00:00 AM

0

> If I were to ask someone for the length of the word apples, I
> would say "What is the length of apples?" In ruby, this would
> be "apples".length.

Length is a property _OF_ "apples".

> If I were to ask someone for the sine of, say, 90, I would
> say "What is the sine of 90?" In ruby, this would be
> Math.sin(90)

Sin is an action _WITH_ 90.

> even though the structure of the sentence is the same as for
> "apples" above.

To me, it's not.

gegroet,
Erik V. - http://www.erikve...


William James

1/9/2007 10:09:00 AM

0

Erik Veenstra wrote:
> > If I were to ask someone for the length of the word apples, I
> > would say "What is the length of apples?" In ruby, this would
> > be "apples".length.
>
> Length is a property _OF_ "apples".
>
> > If I were to ask someone for the sine of, say, 90, I would
> > say "What is the sine of 90?" In ruby, this would be
> > Math.sin(90)
>
> Sin is an action _WITH_ 90.

Very crooked thinking. Consider

%w( the ruby mafia strikes again ).sort

Is 'sort' a property of %w( the ruby mafia strikes again ) ?

>
> > even though the structure of the sentence is the same as for
> > "apples" above.
>
> To me, it's not.

I'll help you out. Consider

What is the zig of zag?
What is the boo of hoo?

See the similarity? However, Ruby, a programming language, doesn't
need to emulate English.

Bira

1/9/2007 11:36:00 AM

0

On 1/9/07, William James <w_a_x_man@yahoo.com> wrote:
> See the similarity? However, Ruby, a programming language, doesn't
> need to emulate English.

The way it's done now (sin(90), cos(90), etc.) makes sense because
it's similar to the actual mathemathical notation I learned back in
grade school. I find it easier to parse than 90.sin, especially when
the argument is actually an expression rather than a constant number.



--
Bira
http://compexplicita.bl...
http://sinfoniaferida.bl...

Ilmari Heikkinen

1/9/2007 1:12:00 PM

0

On 1/9/07, Phrogz <gavin@refinery.com> wrote:
> Daniel Finnie wrote:
> > Why does everything have to be Math.<func>(num)? Isn't num.func more
> > object oriented-ish?
>
> In addition to the answers by others, what would you do for those math
> functions whose arity isn't 1?
>
> Math.methods(false).select{ |n| Math.method(n).arity != 1 }
> #=> ["hypot", "atan2", "ldexp"]
>
> All three take two arguments. While 3.hypot(4) *might* make
> sense--given one leg of a right triangle, how long is the hypotenuse to
> a given other leg--30.atan2( 40 ) makes little sense to me.

hypot and atan are vector methods, so [30, 40].atan2, [3,4].hypot
would be correct in the OO-sense, much like #max and #min are in Array
and not Math (compare to e.g. JavaScript where there is Math.max and
Math.min)

Sammy Larbi

1/9/2007 2:16:00 PM

0

Bira wrote, On 1/9/2007 5:36 AM:
> On 1/9/07, William James <w_a_x_man@yahoo.com> wrote:
>> See the similarity? However, Ruby, a programming language, doesn't
>> need to emulate English.
>
> The way it's done now (sin(90), cos(90), etc.) makes sense because
> it's similar to the actual mathemathical notation I learned back in
> grade school. I find it easier to parse than 90.sin, especially when
> the argument is actually an expression rather than a constant number.
>
>
>

+1