Method and variable name is the same

后端 未结 4 409
难免孤独
难免孤独 2020-12-03 18:29

If both a method and a variable has the same name it will use the variable.

hello = \"hello from variable\"

def hello
  \"hello from method\"
end

puts hell         


        
4条回答
  •  臣服心动
    2020-12-03 18:57

    The ambiguity between local variables and methods only arises for receiverless message sends with no argument list. So, the solution is obvious: either provide a receiver or an argument list:

    self.hello
    hello()
    

    See also

    • How does ruby allow a method and a Class with the same name?
    • Optional parens in Ruby for method with uppercase start letter?

提交回复
热议问题