Issue #16430 has been updated by MikeVastola (Mike Vastola).


sawa (Tsuyoshi Sawada) wrote:
> What is the purpose of writing the code in three different files? It only looks to me that it is making things more complicated.

I was trying to offer a minimal (non)-working example. The benefit arises when there is more code and it makes sense to separate out features into their own files/nested modules.

> A::B.foo and A::C.foo are undefined, and return a NoMethodError, as expected. If you meant A::B#foo and A::C#foo, then they do not make sense because A::B and A::C are modules, not classes.

My mistake. I meant to write `self.foo`. I will fix.

----------------------------------------
Feature #16430: Resultion of constants in enclosing class/module affected by how nested classes/modules are declared
https://bugs.ruby-lang.org/issues/16430#change-83302

* Author: MikeVastola (Mike Vastola)
* Status: Rejected
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I'm not sure if this is intentional (in which case it really isn't documented anywhere, and probably should be) or a bug, but imagine the following code:

``` ruby
# lib/a.rb
module A
  FOO = :BAR
end

# lib/a/b.rb
require_relative '../a'
module A::B
  def foo
    FOO
  end
end

# lib/a/c.rb
require_relative '../a'
module A
  module C
    def foo
      FOO
    end
  end
end

```

If I were to evaluate `A::B.foo`, I would trigger a `NoMethodError (undefined method 'foo' for A::B:Module)`.
However, if I were to evaluate `A::C.foo`, I would get `:BAR`.

This was really confusing to debug because I've been writing the more compact syntax forever where possible without realizing it impacted variable resolution, and it seems kind of bizarre and counter-intuitive that it would work this way.


*  *  *  *

Also, playing with this a bit more, there are some really weird artifacts going on: apparently different methods within the same class/module can have different nestings depending on the context in which they were added to the class?

For example:
``` ruby
module A
  X = 1
end

module A::B
  X = 6
end

module A
  module B::C
    Y = 9
    Z = X + Y # 10
  end
end

module A::B
  module C
    N = X + Y # 15
  end
end

```




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>