From: Daniel Martin Date: 2006-10-17T10:12:38+09:00 Subject: Re: Non-declared variables "Gavin Kistner" writes: > The original post in this thread suggests that omitting declarations of > variables leads to debugging problems for no real gain. My intended > point was that (for me) a lot of Ruby's elegance comes from its > simplicity of expression. Ruby doesn't do anything that I can't do in > Lua or JavaScript, except make my life as a programmer way easier. I think what the OP was getting at is that assignment syntax is identical to declaration syntax, and there's no clear distinction between the two. Therefore, when you want an assignment to be an assignment of an already declared variable, you have nothing checking you for typos. This has nothing to do, by the way, with variable typing. As an example, perl with the "strict" flag on behaves this way, requiring variables to be declared before use and most perl programmers don't find the extra checking cumbersome (in fact, if you go to perlmonks, the first thing they'll ask you to do is to turn on "use strict"). Although variables can be declared in outher ways (it is, after all, perl) most perl programmers just put "my " in front of their first usage of a variable, and that's that. You can declare and assign in one statement, or do them in two. In ruby, I imagine that we'd evolve some syntax like this: class Foo def initialize(name, thing) n @name = name n @thing = thing n @count = 0 end def inc # This line is an error at class definition parse time, because # I typoed a variable name @coutn += 1 end def compute # these four will be initialized to nil n a,b,c,d # I can now use a, b, c, and d as local variables without further # declaration. end end Note that separating declaration from assignment also makes it dirt simple to decide the proper scope of a variable, instead of having to have the switch between ruby 1.9 and ruby 2.0 of various scoping details. I'd welcome such a syntax to ruby - requiring, of course, a per-file strictness switch; no point in killing backwards compatibility - and I think that the extra typing would provide sufficient benefit to be worth it. Note that block parameters are already declared in the || list at the start of the block, so nothing changes in: myArray.each {|a,b| alert(a,b)} although I may want to think carefully about what that block means in the presence of a declared variable called 'b' in the surrounding scope. I generally wouldn't want the variable capture ruby does presently, but some might. -- s=%q( Daniel Martin -- martin@snowplow.org puts "s=%q(#{s})",s.map{|i|i}[1] ) puts "s=%q(#{s})",s.map{|i|i}[1]