[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

instance_eval

Srdjan Marinovic

9/11/2006 11:03:00 AM

hi,

I've got a following problem: when I do

instance_eval do
class T
end
end

(i'm doing this in irb, so self is main)

everything is fine, I get a new class T.
However when I do

instance_eval "class T; end"

I do not get a new class T. even though no errors were reported. Is
there any particular reason for this?

Thanks in advance

srdjan

3 Answers

ts

9/11/2006 11:20:00 AM

0

>>>>> "S" == Srdjan Marinovic <srdjan.marinovic@gmail.com> writes:


Try this

S> instance_eval "class T; end"

p class << self; T end


Guy Decoux

Florian Frank

9/11/2006 11:37:00 AM

0

Srdjan Marinovic wrote:
> I've got a following problem: when I do
>
> instance_eval do
> class T
> end
> end

This works because the context of the block is used to create T.

> (i'm doing this in irb, so self is main)
>
> everything is fine, I get a new class T.
> However when I do
>
> instance_eval "class T; end"
>
> I do not get a new class T. even though no errors were reported. Is
> there any particular reason for this?

T is nested into the singleton class of main, you could use
instance_eval "class ::T; end"
to create a top level class.

But I don't understand why you use instance_eval, why not just eval?

--
Florian Frank

Srdjan Marinovic

9/11/2006 11:46:00 AM

0

hi,

of course eval, I just didn't think of it really, I feel kinda silly:)
Cheers for the explanation I didn't realise that T would be nested
into the main.

Thanks a lot

srdjan