Way to identify ambiguity between local-var name and class method name

Greetings, everyone!

There is wide-used practice in ruby to use attr_accessor (actually, in
Active Record).
But ambiguity between local variable and method names produces
hard-to-catch(silent) errors, for example:

class Person
attr_accessor :address

def remember
address = “Earth” # must use “self.address”
end

def show
puts address
end
end

joe = Person.new
joe.remember
joe.show # nil, must be: “Earth”

Somebody says, that good practice to use self anywhere in class methods,
mean you use it for both obtaining and assignment class variable.
It’s makes code less readable because self overused, and does not help
against miswrite errors (forgotten self on write).

Somebody says, that we must use self only when setting class variable
(as supposed to do). It’s “not so bad” way, but errors still uncatched.

Somebody says, that attr_accessor methods should not be uses inside
class (fine, but what should I do with AR?).

I think that main problem is silent local variable creation. I’ve trying
to find ruby tech to intercept variable creation, but I have fault (I
can track only global variables).
Also, I can’t find ruby warning, when local variable was created, but
not used. It seems good solution - easy to implement, warnings makes
this bugs more “catchable”, total ruby behavior does not changed,
programmers have additional validation tool.

e.g I suggest make a ruby warning when local variable was created, but
not used inside a block. Anyone have same ideas?

PS: Also, there is another ambiguity between method and local variable -
if local variable created with method name. It can be resolved in same
way.

Hi,

Let’s say you solved the problem of ambiguity, so that setters no longer
had to be called with an explicit receiver. Then you wouldn’t know if

a = 1

is an assignment to a local variable a or to an instance variable @a (by
calling the setter of @a).

I don’t find this a good idea. It’s better to make a clear distinction
between setter calls and assignments, like Ruby currently does:

self.a = 1 # accessing the instance variable @a through setter
a = 1 # creating or setting the local variable a

The ambiguity of local variables and getters can also be solved by
simply using parenthesis in method calls:

calling the method a

a()

accessing the local variable a; if it doesn’t exist, Ruby think it’s a

method call and throws an error

a

Many peoply use parenthesis, anyway.

On Mar 29, 2012, at 9:22 AM, Nikolay M. wrote:

Also, I can’t find ruby warning, when local variable was created, but
not used. It seems good solution - easy to implement, warnings makes
this bugs more “catchable”, total ruby behavior does not changed,
programmers have additional validation tool.

Ruby 1.9.3 warns for unused variables if you use ruby -w.

vagrant@ubuntu-10:~$ cat test.rb
def foo
  a = 10
end

foo
vagrant@ubuntu-10:~$ ruby -vw test.rb
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
test.rb:2: warning: assigned but unused variable - a

Ruby < 1.9.3 does not.

Regards,
Florian

It’s better to make a clear distinction between…
Right, it’s a good practice and I placed it in guidelines, but if I
forgot/missed some of this selfs or parenthesis, it would be hard to
identify where is error located. Unused variable warning is good
solution to ident.

Ruby 1.9.3 warns for unused variables if you use ruby -w.
Excellent! I please to know about it. Thanks.