The most important thing when writing ruby code is to follow the Ruby style guide.
In order to follow the Ruby style guide, you should use the rubocop gem.
If in doubt, just use the Rakefile to run the rubocop task.
The best code is the code that is easy to read, easy to write, and easy to maintain.
All code should be tested. Good tests are just as important as good code. An example of a good test:
describe '#foo' do
it 'returns the sum of the arguments' do
expect(foo(1, 2)).to eq 3
end
You may be wondering why the test is not testing the result of the function, but rather the result of the function call. It's because the function call is the test. The test is testing the function.
What does it mean for the test to test the function? Well, it means that the test will pass if the function returns the correct value, and it will fail if the function returns the wrong value.
The correct value is always the value that you expect the function to return. The wrong value is always the value that you do not expect the function to return.
How do we know what the function should return? It depends on what you are testing. For example, if you are testing the foo
method, you should expect the function to return the correct value. If you are testing the bar
method, you should expect the function to return the correct value.
Correctness is a property of the code. What you are testing is the code. The code is the correct value. The code is the wrong value.
One time I wrote a test that was testing the wrong value. I was testing the foo
method, but I was testing the bar
method. I was testing the code that was the wrong value. Don't be like me, don't write tests that test the wrong value.
Anyway, back to the code. When you write the code you should think about the code. If you are writing a method, think about the method. If you are writing a class, think about the class. A class should be a class. A method should be a method.
Some developers argue about the correct way to write a method. Some developers argue about the correct way to write a class. You should not write a class that is a method. You should not write a method that is a class. Opinions differ on this point. I think the correct way to write a class is to be a class. I think the correct way to write a method is to be a method.
When naming a class or method, you should think about the name. If you are naming a class, think about the name of the class. If you are naming a method, think about the name of the method.
If you can't think of a good name for a class or method, just use the name of the class or method.
I hope that this has been useful. If you have any questions, please feel free to ask them in the comments below.