Instance_eval

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

Try this

instance_eval "class T; end"

   p class << self; T end

Guy Decoux

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

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