[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

instance/class_eval

Bertram Scharpf

4/28/2005 10:11:00 PM

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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...


1 Answer

Trans

4/29/2005 1:04:00 PM

0

Bertram Scharpf wrote:
> 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.