I was wondering how Ruby handles this? I know it works, but how?
Ruby has different namespaces for methods and constants, so you can
create both a constant βHβ and a method βHβ. When parsing the expression
H H
Ruby notices that the first βHβ is followed by a parameter, so Ruby
chooses the method βHβ. The second βHβ doesnβt have a parameter, so
Ruby chooses the constant βHβ.
BTW, you can also do the same with methods and local variables:
v = βvβ
def v(a)
p a
end
v v # => βvβ
Note also the following error messages:
x y # => undefined local variable or method `yβ
and
y = 1
x y # => undefined method `xβ
Here you see that βxβ can only be a method, whereas βyβ could be a local
variable or a method.
Regards,
Pit
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.