the12 the12 - 28 days ago 11
Ruby Question

How can Ruby tell the difference between variables and method names if they have the same scope?

How can Ruby tell the difference between variables and methods if both have the same name and the same scope?

Given the scenario:

def something
"33"
end

something = "44"

something # --> calling something (variable or method?)


I am confused because both are called with something. My question is two part:

A) Which would win out: variable or method and why?

B) Is there some way for Ruby to distinguish between the two so you can call something (the variable) and something (the method)?

Answer

variable would be first, but you can call method:

def foo
   33
end

foo = 44

>foo
#=> 44
>foo()
#=> 33
Comments