![]() All lambdas are Procs, but not all Procs are lambdas. It’s also called the “stabby lambda”, as it creates a new Proc instance that is a lambda. (The -> is often called the “stabby proc”. The -> operator creates a new Proc, which is one of Ruby’s function types. Therefore, inside class Foo::Bar, we only have access to the VAL constant declared at the beginning of the script (i.e., outside of any module) with the value 'Global'. However, this lexical scope has no parent (i.e., it is entirely independent of the lexcial scope Foo created earlier) and therefore does not have any access to the contents of the ‘Foo’ scope. However, when we then declare Foo::Bar (i.e., using ::), we are actually creating yet another lexical scope, which is also named Foo::Bar (how’s that for confusing!). Inside Foo, we declare class Bar, which creates another new lexical scope (named Foo::Bar) which also has access to its parent scope (i.e., Foo) and all of its constants. ![]() The above module Foo therefore creates the scope Foo in which the VAL constant equal to 'Foo Local' is defined. The module keyword (as well as the class and def keywords) will create a new lexical scope for all of its contents. Foo::1 will be equal to 'Foo Local' and Foo::2 will be equal to 'Global'. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |