Coirotomas Coirotomas - 2 years ago 61
Ruby Question

How to give precedence to a method over a local variable

I have a method and a local variable with the same name


class A
def x; 5 end

x = 10

I want to refer to the method
without using
. For example, I want:{x} # => 10

to return
. Is that possible?

Answer Source

Yes, in several ways:{x()} # => 5{send(:x)} # => 5

If you only allow x, it is impossible. Local variables have lexical scope, and has precedence over method in case of ambiguity.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download