Math module not found by Ruby, though it seems to be loaded

R

RichardOnRails

When I decided I should come up to speed on Modules, I found a simple
writeup on:
http://www.tutorialspoint.com/ruby/ruby_modules.htm.

The example worked fine as far hooking up the Module routines to the
main code went, but it didn't work because the author didn't finish
defining some of the methods, which I discovered by running the code.
I fleshed out the trig functions by using the math module, but that
bombed.

I posted my code and the error messages at http://www.pastie.org/1596692.

I'm running Ruby 1.8.6 over WinXP-Pro/SP3. I'm stuck on the complaint
that the math module cannot be found

However, the following seems to show that I've got the math module
installed:
K:\>gem list -l math

*** LOCAL GEMS ***

math (0.0.1)
mathml (0.8.1)

So, I can't see what's wrong. Any ideas?

Thanks in Advance,
Richard
 
B

Brian Candler

Just remove the require 'math': the Math module is built-in. Try it in
irb:

irb(main):001:0> Math
=> Math
irb(main):002:0> Math.sin(1)
=> 0.841470984807897

Perhaps in a very old version of ruby you had to require it.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

On Tue, Feb 22, 2011 at 11:55 PM, RichardOnRails <
When I decided I should come up to speed on Modules, I found a simple
writeup on:
http://www.tutorialspoint.com/ruby/ruby_modules.htm.

The example worked fine as far hooking up the Module routines to the
main code went, but it didn't work because the author didn't finish
defining some of the methods, which I discovered by running the code.
I fleshed out the trig functions by using the math module, but that
bombed.

I posted my code and the error messages at http://www.pastie.org/1596692.

I'm running Ruby 1.8.6 over WinXP-Pro/SP3. I'm stuck on the complaint
that the math module cannot be found

However, the following seems to show that I've got the math module
installed:
K:\>gem list -l math

*** LOCAL GEMS ***

math (0.0.1)
mathml (0.8.1)

So, I can't see what's wrong. Any ideas?

Thanks in Advance,
Richard

Math is a class defined in core (
http://ruby-doc.org/core-1.8.6/classes/Math.html), so it is already there,
you already have access to it (its not in the stdlib or anything). So you
can just get rid of the line `require "math"`

However, if you're wanting to use the gem you have installed, you can do
that by first requiring rubygems, which will be found because it is in the
standard library. Rubygems will modify the path in such a way to allow your
ruby to find the math gem when you require it later (assuming gem author
follows conventions). You don't have to do this in newer versions of Ruby.

Also, it doesn't look like you're doing anything in your code that Math
can't already handle, so not sure what you are intending to use the gem for.
 
R

RichardOnRails

Just remove the require 'math': the Math module is built-in. Try it in
irb:

irb(main):001:0> Math
=> Math
irb(main):002:0> Math.sin(1)
=> 0.841470984807897

Perhaps in a very old version of ruby you had to require it.

Thanks, Brain
 
R

RichardOnRails

[Note:  parts of this message were removed to make it a legal post.]

When I decided I should come up to speed on Modules,  I found a simple
writeup on:
http://www.tutorialspoint.com/ruby/ruby_modules.htm.
The example worked fine as far hooking up the Module routines to the
main code went, but it didn't work because the author didn't finish
defining some of the methods, which I discovered by running the code.
I fleshed out the trig functions by using the math module, but that
bombed.
I posted my code and the error messages athttp://www.pastie.org/1596692..
I'm running Ruby 1.8.6 over WinXP-Pro/SP3.  I'm stuck on the complaint
that the math module cannot be found
However, the following seems to show that I've got the math module
installed:
K:\>gem list -l math
*** LOCAL GEMS ***
math (0.0.1)
mathml (0.8.1)
So, I can't see what's wrong.  Any ideas?
Thanks in Advance,
Richard

Math is a class defined in core (http://ruby-doc.org/core-1.8.6/classes/Math.html), so it is already there,
you already have access to it (its not in the stdlib or anything). So you
can just get rid of the line `require "math"`

However, if you're wanting to use the gem you have installed, you can do
that by first requiring rubygems, which will be found because it is in the
standard library. Rubygems will modify the path in such a way to allow your
ruby to find the math gem when you require it later (assuming gem author
follows conventions). You don't have to do this in newer versions of Ruby..

Also, it doesn't look like you're doing anything in your code that Math
can't already handle, so not sure what you are intending to use the gem for.
Math is a class defined in core (http://ruby-doc.org/core-1.8.6/classes/Math.html), so it is already there,
you already have access to it (its not in the stdlib or anything). So you

Thanks, Josh
Also, it doesn't look like you're doing anything in your code that Math
can't already handle, so not sure what you are intending to use the gem for.

The example I copied had methods for Trig functions but the methods
didn't return any floating point values, just trash as far as I was
concerned. So I provided Math.sin(x) for the def sin(x) method.

Best wishes,
Richard
 
R

RichardOnRails

[Note:  parts of this message were removed to make it a legal post.]

When I decided I should come up to speed on Modules,  I found a simple
writeup on:
http://www.tutorialspoint.com/ruby/ruby_modules.htm.
The example worked fine as far hooking up the Module routines to the
main code went, but it didn't work because the author didn't finish
defining some of the methods, which I discovered by running the code.
I fleshed out the trig functions by using the math module, but that
bombed.
I posted my code and the error messages athttp://www.pastie.org/1596692..
I'm running Ruby 1.8.6 over WinXP-Pro/SP3.  I'm stuck on the complaint
that the math module cannot be found
However, the following seems to show that I've got the math module
installed:
K:\>gem list -l math
*** LOCAL GEMS ***
math (0.0.1)
mathml (0.8.1)
So, I can't see what's wrong.  Any ideas?
Thanks in Advance,
Richard

Math is a class defined in core (http://ruby-doc.org/core-1.8.6/classes/Math.html), so it is already there,
you already have access to it (its not in the stdlib or anything). So you
can just get rid of the line `require "math"`

However, if you're wanting to use the gem you have installed, you can do
that by first requiring rubygems, which will be found because it is in the
standard library. Rubygems will modify the path in such a way to allow your
ruby to find the math gem when you require it later (assuming gem author
follows conventions). You don't have to do this in newer versions of Ruby..

Also, it doesn't look like you're doing anything in your code that Math
can't already handle, so not sure what you are intending to use the gem for.
so not sure what you are intending to use the gem for.

I mis-read this line when I previously replied. I thought I had to
install the gem because I didn't know that Math was included in the
Kernel. I guess I'll leave it installed and use your "rubygems"
invocation if I ever think I have to use it.

But I benefited from your comment about 'rubygems' in that I saw it
used often but never had a clue as to why it was used. So, thanks for
the "clue".

Best,
Richard
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top