Writing ruby the right way
1) Write Code that Looks Like Ruby
- Ruby code is clear and self-explanatory. Aim for concise, obvious code that doesn’t need lots of comments to understand.
- For included comments, try for only ones explaining especially complex code or broad explanations of how to use the code.
- Only spaces, no tabs. Indents are two spaces.
- Use CamelCase for class names, and snake_case for everything else.
- For methods without any arguments or simple conditionals, don’t use parenthesis.
- Unless something is simple enough to be just one line, don’t cram multiple lines of code into a single one.
- Only make block statements one line if it contains one line.
The only exception to any of these rules would be to make Ruby code more readable.
- Methods that answer a yes/no or true/false question often end with a
?, such as
valid?to ask if a method or value is valid.
2) Choose the Right Control Structure
ifstatement works as you’d expect, looking for a value it’ll see as
true. If you need the opposite effect, use the
unlessstatement instead. Shorter and easier to understand.
whilestatement has a similar counterpoint with the
- Simple logic statements, such as those assigning variables, can be collapsed into a single line. This saves space and is easier to understand.
If you need a simple “if, else” statement for something, use the ternary operator like so:
each instead of
for basically calls the
each method itself, creating an avoidable inefficiency.
Case statements are also good for more complex code reliant on, or assigning, a range of different values. These lines can also be collap
In Ruby boolean logic, only
nilare considered false values. Two things are false, everything else is true. So when testing for truth, don’t test for something to equal a specific value.
If you want to define a value to a variable but aren’t sure if it already has one, use this trick. If
@nameis nil or false, it’ll use the following string.