When dealing with large numbers you can use an underscore (in place of a comma) to make the numbers more readable and easier to write.
1 2 |
|
When dealing with large numbers you can use an underscore (in place of a comma) to make the numbers more readable and easier to write.
1 2 |
|
A common thing to do in ruby is to share functionality among classes. Since ruby doesn’t support multiple inheritance, we simply import code by mixing in modules. So instead of classes having a crazy inheritance tree, they can have includes and/or extends at the top of the class. But what is the difference between include and extend?
They both mix in functionality into a class, but they are added to different places. Include mixes functionality into instances of a class, whereas extend mixes functionality into a class itself.
Take a look at the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
And of course with ruby being dynamic and all, you can include and/or extend modules at run time
1 2 3 4 5 6 7 8 9 10 |
|
If you want more information on include and extend check out the following links:
A performance trick that I use a lot, especially in models, is memoizing methods that are expensive. Usually
these are methods that hit the database, make external API calls, involve longs loops, etc. Suppose we have a
model Song that has a genre column in the database and a method jazz that queries for all songs that have the
genre jazz
. The model would look something like this:
1 2 3 4 5 |
|
Now this looks all fine and dandy but what if that method gets called several times? It will cause a query every time and will slow down performance.
We can trade space for time by saving the results in an instance variable. So the first time the method is called, a query to the database will be executed. However all subsequent calls to the method will not query the database and will just return the saved result. That would look like this:
1 2 3 4 5 |
|
A common problem with caching is forgetting to clear it. Whenever a new song gets added, deleted, or modified, we’re going to need to clear the @jazz
instance variable so that when the jazz method is called, it will get those new songs. Easy enough, we can just add callbacks to clear the cache after saving. Check it out:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
So I have another boy on the way and I was trying to think up of names so I created a ruby script to pick a random name out of a file beginning with a letter that you provide as an argument. So you’d call the script like this:
1 2 |
|
1 2 3 4 5 6 7 8 9 10 11 |
|
What’s cool about it is this line
1
|
|
Enumberable#group_by
will group elements in an array by the result of the block passed in. In this case, we’re grouping by
the first letter.
1 2 |
|
You can list tags by running this:
1
|
|
Suppose we see a tag called v1.0.1 that we want to remove. We’d simply remove it like this:
1 2 |
|
The first line will remove the tag locally and the second line will remove the remote tag.
Recently I’ve had to retrieve information from a set of remote servers. Using the for loop in the terminal, I was able to get all the information I needed in one line which was very convenient. I used the following code snippet:
1
|
|
To have the rails log output to the console you can add this to your ~/.irbrc
file:
1 2 3 4 5 6 7 8 9 |
|
This only enables the log on the machine you’re currently on. If you want to enable logging to the console as a project
wide feature so that anyone who checks out the project and runs the console also gets logging, then you can put this in
your environment.rb
file (or an environment config file like development.rb
):
1 2 3 |
|
Suppose we have a remote and local branch called foo. To delete it locally, just run:
1
|
|
You can verify that it has been deleted by listing your local branches. To do that, run:
1
|
|
To delete the remote branch run:
1
|
|
To list all branches (including remotes) run:
1
|
|