instance_eval

S

Srdjan Marinovic

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
 
F

Florian Frank

Srdjan said:
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?
 
S

Srdjan Marinovic

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
 

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,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top