From: Gary Wright Date: 2010-02-06T03:57:12+09:00 Subject: Re: function lookup in ruby On Feb 5, 2010, at 1:42 PM, Cnm Cnm wrote: > Hi all, > I was wondering what happened in the background of following snippet. > First I define a method v1, and I am able to call v1. Then I redefine v1 > as an array; after that I can only call v1 as a function by calling > v1(). How does ruby look up v1 as a function and variable? Thanks > > irb(main):001:0> def v1 > irb(main):002:1> puts 'printing from method v1' > irb(main):003:1> end > => nil > irb(main):004:0> v1 > printing from method v1 At this point the only thing named 'v1' is a method so the method is called. > => nil > irb(main):005:0> v1 = [] > => [] Now there are two things named 'v1' a method and a local variable. Name resolution prefers local variables so... > irb(main):006:0> v1 > => [] The local variable is referenced > irb(main):007:0> v1() > printing from method v1 > => nil By adding the explicit parens you've given Ruby a hint that you want to call a method and not lookup a local variable. Alternatively you could do: self.v1 which would also reference the method and not the local variable. Gary Wright