Require Math

L

Lindsay Boyd

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
 
M

Mike Stok

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 <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
D

Dave Burt

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
 
L

Lindsay Boyd

2.sqrt #=> 1.4142135623731

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

Lindsay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

require and include?? 7
"Load" Works, But "Require" Doesn't? 4
linux -- require question 2
require problem 3
Ruby 1.9.2 require issue 2
require command problem 4
require wackyness 2
irb: require returns false? 1

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,668
Latest member
SamiraShac

Latest Threads

Top