Brain DamagedWhen I see an IF statement in any programming language, it still comes with a THEN. A FOR statement still comes with a NEXT. My first language was BASIC (learned on a Commodore PET in Junior High), and it's still in my head. As Edsger Dijkstra
said "It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration." I believe that most of my mental mutilation has been repaired, but some residual damage is apparently there.
In the same essay, Dijkstra also says "The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities." Since switching to Ruby and Rails for the majority of my programming work, I have repeatedly found this statement to be true. Other than some long-ago assembly language programming, my past work has been done in a relatively similar family of languages (C#, Delphi, VB, etc.), causing me to settle into predictable and comfortable thought patterns.
Brain RepairTo dive deeper into Ruby and change some old habits I've been exloring the following with satisfying results:
- Array and Ennumerable methods: (in the Pickaxe book or here). Arrays include the methods of Ennumerable, so both are worth knowing. Since ActiveRecord returns arrays for multiple record searches, it is good to know more than for and .each to work with the models.
- Ruby modules: It's beautiful to have something other than inheritance to share behavior between classes.
- Method parameters: Default parameters, hashes as parameters, variable length argument lists, using a code block as a parameter, using a code block at the end of the method call.
- Dynamically adding methods to classes and objects.
It is productive to examine the Rails source code, scaffold-generated code, code from plugins and engines, etc. line by line. When I pick someone elses code apart, making sure I understand every method, operator, etc. used, I learn a lot -- fast. It is painful but effective.