Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > instance_eval

Reply
Thread Tools

instance_eval

 
 
Srdjan Marinovic
Guest
Posts: n/a
 
      09-11-2006
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

 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      09-11-2006
>>>>> "S" == Srdjan Marinovic <> writes:


Try this

S> instance_eval "class T; end"

p class << self; T end


Guy Decoux

 
Reply With Quote
 
 
 
 
Florian Frank
Guest
Posts: n/a
 
      09-11-2006
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

 
Reply With Quote
 
Srdjan Marinovic
Guest
Posts: n/a
 
      09-11-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
instance_eval lopex Ruby 10 08-17-2004 03:27 PM
instance_eval from C ext Joel VanderWerf Ruby 3 05-02-2004 11:00 PM
instance_eval and constants Aron Griffis Ruby 2 02-24-2004 11:15 PM
instance_eval and Class Variables Tim Bates Ruby 0 08-06-2003 06:05 AM
instance_eval("class") error/bug??? walter@mwsewall.com Ruby 1 07-17-2003 05:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57