[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Require Math

Lindsay Boyd

3/23/2006 11:38:00 AM

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

--
Posted via http://www.ruby-....


4 Answers

Mike Stok

3/23/2006 11:44:00 AM

0


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...

The "`Stok' disclaimers" apply.






Lindsay Boyd

3/23/2006 11:51:00 AM

0

Thanks Mike - I was trying '2.sqrt'. No wonder that didn't work!

--
Posted via http://www.ruby-....


Dave Burt

3/23/2006 1:58:00 PM

0

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


Lindsay Boyd

3/23/2006 11:24:00 PM

0

> 2.sqrt #=> 1.4142135623731

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

Lindsay

--
Posted via http://www.ruby-....