Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method and variable name is the same

Tags:

ruby

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 hello

Is it possible somehow to use the method instead without changing name?

like image 992
never_had_a_name Avatar asked Sep 18 '10 11:09

never_had_a_name


2 Answers

Try this:

puts hello()
like image 58
Nakilon Avatar answered Sep 19 '22 14:09

Nakilon


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?
like image 44
Jörg W Mittag Avatar answered Sep 19 '22 14:09

Jörg W Mittag