instance/class_eval

B

Bertram Scharpf

Hi, experts,

how can I find out what my current class context is?
I say:

class C ; end
C.instance_eval { def f ; "f" ; end }
C.f
#=> "f"

class D ; end
D.class_eval { def f ; "f" ; end }
D.new.f
#=> "f"

Obviously the method will be put into another context
either. I would like to be able to ask what the context is
_before_ I define the method. `self' seems to be the same in
both cases.

Sorry, it's my itch that I want to know the whole truth.

Thanks in advance.

Bertram
 
T

Trans

Bertram said:
Hi, experts,

how can I find out what my current class context is?
I say:

class C ; end
C.instance_eval { def f ; "f" ; end }
C.f
#=> "f"

class D ; end
D.class_eval { def f ; "f" ; end }
D.new.f
#=> "f"

Obviously the method will be put into another context
either. I would like to be able to ask what the context is
_before_ I define the method. `self' seems to be the same in
both cases.

Sorry, it's my itch that I want to know the whole truth.

I assume you mean:

C.instance_eval { self } == C.class_eval { self }

And you are right, self is the same for both contexts. I believe the
question is the same as asking if it is possible to detect the
idioclass b/c the idioclass of the class_eval context is the
instance_eval context.

class C
class << self
# this is C.instance_eval { self }
end
# this is C.class_eval { self }
end

That being the case, see the new thread: determining when inside 'class
<< self'.

T.
 

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,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top