Hi,

At Tue, 17 Nov 2009 13:18:29 +0900,
Yehuda Katz <wycats / gmail.com> wrote:
> I agree that we should have both, but I think that the lexical scope case
> should win. Consider the case of RSpec:
> 
> describe "Date" do
>   it "equals itself" do
>     Date.today.should == Date.today
>   end
> end
> 
> If we use dynamic scope first, then if RSpec adds Spec::Date, it will
> suddenly break this spec. Lexical scope is more intuitive, and is expected
> by many normal uses today. On the other hand, we want to be able to access
> class variables in the eval'ed scope. I think a good solution is to add the
> dynamic scope _after_ the lexical scope, rather than _before_ it.

My patch first searches the constants of the receiver of class_eval, and
if it fails, then searches outer classes or modules.  As a result, a
constant in the receiver may hide a constant in an outer class or
module.

My patch is useful if you know the receiver of class_eval, but you
don't know it, right?

Back to the example of MimeResponds:

module ActionController
  module MimeResponds
    extend ActiveSupport::Concern

    included do
      inheritable_accessor :responder, :mimes_for_respond_to,
:instance_writer => false
      self.responder = Responder
      clear_respond_to
    end
  end
end

class Foo
  Responder = "this is not a responder which MimeResponds requires"

  include ActionController::MimeResponds
end

The author of MimeResponds doesn't know that Foo defines Responder in
it, and it breaks the code (with my patch).

Did I catch your point?

If so, I agree that my patch is not useful in this situation.

However, I guess "to add the dynamic scope _after_ the lexical scope,
rather than _before_ it" doesn't affect class variable lookup, because
only the first element of Module.nesting is used during class variable
lookup.

I guess we have four options now:

(1) As you suggested, revert to the behavior of Ruby 1.8, and add a
    new method (or flag) to enable the new behavior.
(2) Separate class variable lookup from constant lookup, and revert
    constant lookup to the behavior of Ruby 1.8.
(3) Change nothing.  You should always use the fully qualified form of
    constant lookup.
(4) Apply my patch.  You should still use the fully qualified form of
    constant lookup, if you don't know the receiver of class_eval.

-- 
Shugo Maeda <shugo / ruby-lang.org>