April 6th, 2015
(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: email@example.com
That is, the code:
is functionally equivalent to:
# Create a new Module instance and assign it to the Foo constant
Foo = Module.new
# Reopen the Module instance assigned to the constant Foo for modification
# Do stuff in here to add functionality to the Foo module
# Since Foo is already defined, we can get its object_id.
This certainly doesn’t make sense coming from a compiled language standpoint (after all, how do you know what Cashier is if you haven’t finished its definition yet?), but Ruby’s interpreted nature means that it tends to treat things like module and class definitions a bit more loosely, which is what is permitting this behavior.