Forum: Ruby Require Math

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ae03102311939454d2b7f1ca3139fd97?d=identicon&s=25 Lindsay Boyd (Guest)
on 2006-03-23 12:38
I need to access the sqrt method in my rails app. I believe I need the
Math library for this:

irb(main):010:0> require "Math"
LoadError: no such file to load -- Math

Could someone tell me where I download the Math library from, and where
I should place it on my file system? I can't seem to find any
documentation explaining this.

Lindsay
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2006-03-23 12:44
(Received via mailing list)
On 23-Mar-06, at 6:38 AM, Lindsay Boyd wrote:

> I need to access the sqrt method in my rails app. I believe I need the
> Math library for this:
>
> irb(main):010:0> require "Math"
> LoadError: no such file to load -- Math
>
> Could someone tell me where I download the Math library from, and
> where
> I should place it on my file system? I can't seem to find any
> documentation explaining this.

It is already there, you can call it in a couple of ways:

puts Math.sqrt(2)

or if you want a lot of Math methods you can use include to get the
Math module's methods without the Math prefix:

include Math
puts sqrt(2)

You should read up on what include does before using it.

Hope this helps,

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
Ae03102311939454d2b7f1ca3139fd97?d=identicon&s=25 Lindsay Boyd (Guest)
on 2006-03-23 12:50
Thanks Mike - I was trying '2.sqrt'. No wonder that didn't work!
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 Dave Burt (Guest)
on 2006-03-23 14:58
(Received via mailing list)
Lindsay Boyd:
> Thanks Mike - I was trying '2.sqrt'. No wonder that didn't work!

Yeah, I've always wanted to do that kind of thing, too... let's try
this:

class Numeric
  Math.methods(false).each do |m|
    define_method(m) {|*args| Math.send(m, self, *args) }
  end
end

2.sqrt  #=> 1.4142135623731

Floating-pointilicius!

Cheers,
Dave
Ae03102311939454d2b7f1ca3139fd97?d=identicon&s=25 Lindsay Boyd (Guest)
on 2006-03-24 00:23
> 2.sqrt  #=> 1.4142135623731

That's really elegant. I can see why Ruby has so many fans :-)

Lindsay
This topic is locked and can not be replied to.