From: Jim Freeze Date: 2006-05-07T13:13:32+09:00 Subject: Re: A major ruby annoyance! On May 6, 2006, at 6:00 PM, Talha Oktay wrote: > I am a newbie in Ruby. I have read several books including pickaxe > and wrote > several thousands lines of code. So far, as an ex perl user, I have > found > the major annoyance with ruby is the lack of declaration of variables. > Automatic creation, in perl jargon autovification is the common > feature of > many high level languages such as perl, paython, visual basic etc. > but most You can customize autovivification in Ruby. Consider the Hash h1 = Hash.new { |h,k| h[k] = 0 } h1[:a] #=> 0 h2 = Hash.new { |h,k| h[k] = 1 } h2[:a] #=> 1 h3 = Hash.new { |h,k| h[k] = [] } h2[:a] << "alpha" #=> ["alpha"] l = lambda { |h,k| h[k] = Hash.new &l } h4 = Hash.new &l h[:a][:b][:c] = 5 #=> { :a => { :b => { :c => {} }}} > of the these languages have seen that this not necessarily a good > thing and > created flags, commands to prohibit it if the developer wants to. I > really > wish ruby had one. Last several hours I spent debugging a > malfunctioning > code just to see mistyped variable name was causing all the > problem. I had > to put many log statements which polluted to the code. You can try to up the warning level. It might also help to make smaller methods. I you want, post your code and let some people take a look to give you some pointers on how to prevent this type of thing. > I guess in Pickaxe book, unit testing is presented as the cure for > this > problem which I do not agree. I can not write a test code for every > peace of > little functionality, besides I am eager to make the compiler or > interpreter > to work for me. Why not tell the interpreter to check the code, > instead of > writing very detailed test programs. I am not a very good typist > and I often > mistype variable names, often I had to depend on vim's auto completion > feature. But I do not want to depend to an editor or an IDE for this. The compiler can't think for you. In Ruby, a little bit of code can do a lot, in fact, a lot more than a compiler can guess at. Consider: [1,2,3].inject(0) { |prod,x| prod * x } [1,2,3].inject(1) { |prod,x| prod * x } How is an interpretive language supposed to know (or take the time to know) that the second case is what you want, not the first? > I have also found the ruby's -w flag does not work as I perl -w > flag does. > In perl warnings are really descriptive. You can even use a module > called > diagnostics which almost teaches to fix the problem that crashes > program or > generates warning etc. I often do not use -w as because I receive > warnings > from libraries that my code includes or warnings I do not > understand at all. > > May be I am the only one who is bothered with these. I do not know. Some mental reprogramming takes time. We can't brain wash you overnight. ;) Jim Freeze