Evals confusion

Hi.

Part of my unit tests need to define new classes to test the code I generate
for those newly defined classes. I'm not sure what's a good way to do this.

Is
    eval "class A < B; end"
a reasonable way to define these classes inside a test method? It's the
closest to what my "user code" will be doing. If so, how can I clean up at
the end of the test? const_set gives warnings.

Or, should I be doing
    c = Class.new
    # do stuff with c

Thanks.

itsme213 wrote:

Part of my unit tests need to define new classes to test the code I generate
for those newly defined classes. I'm not sure what's a good way to do this.

Is
    eval "class A < B; end"
a reasonable way to define these classes inside a test method? It's the
closest to what my "user code" will be doing. If so, how can I clean up at
the end of the test? const_set gives warnings.

Or, should I be doing
    c = Class.new
    # do stuff with c

I'd prefer this a lot over the above. You can still use const_set() though the redefinition warning won't go away.

> Or, should I be doing
> c = Class.new
> # do stuff with c

I'd prefer this a lot over the above. You can still use const_set()
though the redefinition warning won't go away.

Is there a way to give a name to the class? My code generation uses the
class name. I tried assigning it to a constant but get an error:
    "dynamic constant assignment"

itsme213 wrote:

Is there a way to give a name to the class? My code generation uses the
class name. I tried assigning it to a constant but get an error:
    "dynamic constant assignment"

Yup, via Object.const_set("Foo", Class.new).

"Florian Gross" <flgr@ccan.de> schrieb im Newsbeitrag
news:2va204F2ie5e5U1@uni-berlin.de...

itsme213 wrote:

> Is there a way to give a name to the class? My code generation uses

the

> class name. I tried assigning it to a constant but get an error:
> "dynamic constant assignment"

Yup, via Object.const_set("Foo", Class.new).

Direct assignment works, too:

Foo = Class.new String

=> Foo

Foo.superclass

=> String

Foo.ancestors

=> [Foo, String, Enumerable, Comparable, Object, Kernel]

Foo.name

=> "Foo"

Kind regards

    robert

Both const_set and direct assignment have the class created first, so the
"inherited" callback has already been completed. I guess to test my usage of
that callback I have to resort to one of the evals.

Thanks for the help.

"Robert Klemme" <bob.news@gmx.net> wrote in message
news:2vbepuF2g6vfnU1@uni-berlin.de...

···

"Florian Gross" <flgr@ccan.de> schrieb im Newsbeitrag
news:2va204F2ie5e5U1@uni-berlin.de...
> itsme213 wrote:
>
> > Is there a way to give a name to the class? My code generation uses
the
> > class name. I tried assigning it to a constant but get an error:
> > "dynamic constant assignment"
>
> Yup, via Object.const_set("Foo", Class.new).

Direct assignment works, too:

>> Foo = Class.new String
=> Foo
>> Foo.superclass
=> String
>> Foo.ancestors
=> [Foo, String, Enumerable, Comparable, Object, Kernel]
>> Foo.name
=> "Foo"
>>

Kind regards

    robert

Robert Klemme wrote:

class name. I tried assigning it to a constant but get an error:
   "dynamic constant assignment"

Yup, via Object.const_set("Foo", Class.new).

Direct assignment works, too:

From inside methods it only does when you wrap it inside the eval. I think the point was avoiding eval statements.

"Florian Gross" <flgr@ccan.de> schrieb im Newsbeitrag
news:2vcgefF2ipojmU1@uni-berlin.de...

Robert Klemme wrote:

>>>class name. I tried assigning it to a constant but get an error:
>>> "dynamic constant assignment"
>>Yup, via Object.const_set("Foo", Class.new).
> Direct assignment works, too:

From inside methods it only does when you wrap it inside the eval. I
think the point was avoiding eval statements.

Ah, yes, you're right. Sorry for the noise.

    robert