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.
···
--
Posted via http://www.ruby-forum.com/.