Returning a class

Hello,

Is it possible to generate a class at run-time and return it?

I’ve been working on a Polynomial class. But you can define polynomials
over any ring, not just the reals. So I had this idea:

Create a CommRing class (commutative ring). You inherit from that class
and create a ring.

class R < CommRing

definning @zero, @id, @minus_id, + and *

end

You automatically get the class method R.poly which corresponds to the
polynomial ring over R.

How would I do this?

Thanks.

···


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Is it possible to generate a class at run-time and return it?

···

----- Original Message -----

Not exactly sure what you are looking for, but maybe this will help you? If
not, could you give a little more information about what you want your
auto-generated class to look like?

Instead of:

class Foo < Bar
end

you could try:

Foo = Class.new Bar

Class names are just constants, after all. If you want it to be anonymous,
just don’t set it to Foo.

Of course, you could always generate the Ruby code you want in a string, and
eval that, but there are usually easier ways to do what you want.

Chris

Is it possible to generate a class at run-time and return it?

Not exactly sure what you are looking for, but maybe this will help you? If
not, could you give a little more information about what you want your
auto-generated class to look like?

What you wrote helps, I think. I’ll try to implement it. But to answer your
question, this is what I want:

I put together a Polynomial class. You can use it like this:

$ irb

require ‘poly’
=> true
include Math::Polynomial
=> Object
puts 7 * X3 + 4 * X2 - 14 * X**3 - 5 * X **17 - 23
17 3 2

  • 5 X - 7 X + 4 X - 23
    => nil

puts (X - 1) * (X + 1)
2
X - 1
=> nil

After I’ve done some more work, I’d like to extend this so that it works over any
ring (not just real numbers). I think that it would be neat.

I also thought that I could make a Frac() class that gives you the fraction field
of a ring (class). For instance, Frac(Integer) would be the same thing as
Rational.

If I define a similar Polynomial, I could use Frac(Polynomial(Integer)) to define
a class whose elements are of form p(x)/q(x)

I don’t know how I would do this. I was just investigating the possibility.

···

----- Original Message -----


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137