In Ruby, how does a class defined in a module know the module's constants?

寵の児 提交于 2019-12-03 17:24:05

If you reference a class constant, Ruby will look for it first in the same module, and then on the root if it's not found there.

So, since both Seat and Wheel are in the Car module, if you look for Wheel, it will first look for Car::Wheel, and then for ::Wheel. Since Car::Wheel exists, you get that reference.

You are looking for information on Ruby's Constant Lookup rules. Details surrounding this changed in Ruby 1.9, and then changed again in Ruby 1.9.2. You can read some discussion on this on the ruby-core mailing list.

Here is an excellent review of some of differences between Ruby 1.8.x, Ruby 1.9, and Ruby 1.9.2: http://jfire.posterous.com/constant-lookup-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!