From: Dale Martenson Date: 2002-04-01T03:12:21+09:00 Subject: RE: Why isn't Math object-oriented? This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C1D8DE.A80DA390 Content-Type: text/plain; charset="iso-8859-1" > Tobias DiPasquale wrote: > On Sat, 30 Mar 2002 15:43:41 -0500, Bil Kleb wrote: > > Math.sin(number) > > > > which looks alot like Java. > > > > What happened? Why did the idiom change? Why isn't Math implemented as > > a mixin for the Numeric class a'la Enumerable or Comparable or Kernal, > > etc.? > > Good question. I tried to do this: > > > class Fixnum > include Math > end > > 354.sin > > > However, this failed because all of Math's methods are class methods > This doesn't seem to make any sense, since Math is a Module. Anyone > know the reasoning behind this one? Matz? I agree this is a great philosophical question. If you want to treat math in a more object oriented way, you could do the following. I am not sure how you would want to handle methods that require more than one parameter. class Fixnum def method_missing(methId) m = Math.method(methId) if m.arity == 1 then m.call(self) else raise(NameError, "undefined method '#{methId.id2name}' for class 'Fixnum'") end end end print "Math.sin(90) = #{Math.sin(90)}\n" print "90.sin = #{90.sin}\n" print "90.atan2 = #{90.atan2}\n" ------_=_NextPart_001_01C1D8DE.A80DA390 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable RE: Why isn't Math object-oriented?

> Tobias DiPasquale wrote:
> On Sat, 30 Mar 2002 15:43:41 -0500, Bil Kleb = wrote:
> >  Math.sin(number)
> >
> > which looks alot like Java.
> >
> > What happened? Why did the idiom = change?  Why isn't Math implemented as
> > a mixin for the Numeric class a'la = Enumerable or Comparable or Kernal,
> > etc.?
>
> Good question. I tried to do this:
>
> <snip>
> class Fixnum
>       include = Math
> end
>
> 354.sin
> </snip>
>
> However, this failed because all of Math's = methods are class methods
> This doesn't seem to make any sense, since Math = is a Module. Anyone
> know the reasoning behind this one? = Matz?

I agree this is a great philosophical question. If = you want to treat math in a more object oriented way, you could do the = following. I am not sure how you would want to handle methods that = require more than one parameter.

class Fixnum
  def method_missing(methId)
    m =3D Math.method(methId)
    if m.arity =3D=3D 1 then
      m.call(self)
    else
      raise(NameError, = "undefined method '#{methId.id2name}' for class = 'Fixnum'")
    end
  end
end

print "Math.sin(90) =3D = #{Math.sin(90)}\n"
print "90.sin =3D #{90.sin}\n"
print "90.atan2 =3D #{90.atan2}\n"

------_=_NextPart_001_01C1D8DE.A80DA390--