Just some notes after reading “Eloquent Ruby” chapter one.
indent
use two spaces per indent
comments
There are good reasons for adding comments to your code, the best being to explain how to use your software masterpiece. These kinds of “how to” comments should focus on exactly that: how to use the thing. Don’t explain why you wrote it, the algorithm that it uses, or how you got it to run faster than fast. Just tell me how to use the thing and remember that examples are always welcome.
Sometimes it’s also wise to include a “how it works” explanation of particularly complicated bits of code. Again, keep this kind of explanation separate from the “how to”.
The occasional in-line comment can also help.
Remember, good code is like a good joke: It needs no explanation.
Camels for Classes, Snakes Everywhere Else
- Almost everything in this context means methods, arguments, and variables, including instance variables use snake case.
- Class names are camel case.
- Constants use all uppercase, punctuated by underscores.
Parenthess
both in method definitions and calls surround parenthess.
don’t surround parenthess:
- conditions in control statements
- defining or calling a method with no parameters
- puts, instance_of?, and your right feeling
Look set.rb in your ruby lib path for more ruby conventions.
Naming
- end the name of a method that answers a yes/no or true/false question with a question mark
- reserve ! to adorn the names of methods that do something unexpected, or perhaps a bit dangerous