[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

evals confusion

Its Me

11/8/2004 7:30:00 PM

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.


7 Answers

Florian Gross

11/8/2004 7:44:00 PM

0

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.

Its Me

11/8/2004 7:49:00 PM

0


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




Florian Gross

11/8/2004 8:08:00 PM

0

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

Robert Klemme

11/9/2004 8:52:00 AM

0


"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

Its Me

11/9/2004 2:10:00 PM

0

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
>


Florian Gross

11/9/2004 6:27:00 PM

0

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.

Robert Klemme

11/10/2004 8:26:00 AM

0


"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