From: dblack@... Date: 2007-05-11T06:45:58+09:00 Subject: Re: self in blocks Hi -- On Fri, 11 May 2007, Vasco Andrade e silva wrote: > Hi > > This seems to me somehow inconsistent: > > class A; end > > # 1) > # output A > A.class_eval { puts self } > > # 2) > # output main (in irb) > [1].each { puts self } > > The "problem" to me is that in 1) 'self' seems to refer to the context > where yield is called, where in 2) seems to refer to the context in > execution. > What should i expect from 'self'`s behaviour? self doesn't really behave; it just plays the role it's assigned to play in different circumstances. class_eval sets self to the receiver (it's specially designed for that purpose -- basically a programmatic rather than declarative way to enter a class definition block). In general, though, self doesn't change when you enter a code block. That's why self is still main inside your second block. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)