Why is there a seperate Math class?

Let me try to make everybody happy :

class Numeric
  Math.methods(false).each { |m|
    sm = Math.method(m)
    if Math.method(m).arity == 1
      define_method m do
        sm.call self
      end
    end
  }
end

irb(main):001:0> require 'math_extend'
=> true
irb(main):002:0> 34.cos
=> -0.848570274784605
irb(main):003:0> Math.cos 34
=> -0.848570274784605

Regards,
Maël Clérambault

There are inconsistencies that annoy me with Ruby, though, such as
Math.sin(x) but x.abs

x.abs seems to me a convenience method.

If you're using irb in math mode or something like this, this might
make your functions look prettier:

module Math
  def abs(x)
    x.send(:abs)
  end
end

=> nil

include Math

=> Object

abs(10)

=> 10

abs(-10)

=> 10

abs(-10.2114125)

=> 10.2114125

···

On 1/10/07, Vidar Hokstad <vidar.hokstad@gmail.com> wrote:

Maël Clérambault <ml-ruby@clerama.be> writes:

Let me try to make everybody happy :

class Numeric
  Math.methods(false).each { |m|
    sm = Math.method(m)
    if Math.method(m).arity == 1
      define_method m do
        sm.call self
      end
    end
  }
end

irb(main):001:0> require 'math_extend'
=> true
irb(main):002:0> 34.cos
=> -0.848570274784605
irb(main):003:0> Math.cos 34
=> -0.848570274784605

Regards,
Maël Clérambault

You gotta love it! A language which really does allow you to easily modify it
to what you want it to look like and fit with houw yo want to think about the
problem. Now nobody is right or wrong - its just a question of taste and
individual thinking style.

Tim

···

--
tcross (at) rapttech dot com dot au