ruby - Why does a method call need to be disambiguated when it can in principle be a constant? -


method calls can omit receiver , parentheses arguments:

def foo; "foo" end foo # => "foo" 

in case above, foo ambiguous between method call , reference potential local variable. in absence of latter, interpreted method call.

however, when method name can in principle constant name (i.e., when starts capital letter, , consists of letters), seems need disambiguation.

def foo; "foo" end foo # => nameerror: uninitialized constant foo foo() # => "foo" self.foo # => "foo" 

why case? why method call need explicitly distinguished reference constant under absence of constant same name?

the set of local variables in scope @ given point in program defined lexically , can determined statically, parse time. so, ruby knows before runtime local variables in scope , can distinguish between message send , local variable dereference.

constants looked first lexically, via inheritance, i.e. dynamically. not known constants in scope before runtime. therefore, disambiguate, ruby assumes it's constant, unless isn't, i.e. takes arguments or has receiver or both.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -