Constant search in Ruby uses a lexical region that intersects the current region and contains modules. The search path can be viewed using Module.nesting
> module Out > module In > puts Module.nesting > end > end Out::In # gets searched for a given constant first Out
The search remains unchanged inside the block to enable closure behavior, even for class_eval .
However, in Ruby 1.9, the receiver for instance_eval , instance_exec , class_eval and class_exec was added to the search path, which means that all constant references will first be detected in the receiver area.
Yehuda Katz raised a question with reference to a significant breakdown:
Consider the RSpec case:
describe "Date" do it "equals itself" do Date.today.should == Date.today end end
If you use the dynamic scope first, then if RSpec adds Spec :: Date, it will suddenly break this specification. The lexical area is more intuitive, and expected today by many normal needs.
The behavior subsequently returned to 1.8.
source share