25 Feb 2015
git log is fairly useful.
It’s even more useful with a few other commands:
git log --all - Include all branches in output
git log --graph - Include visual depiction of branchesall branches in output
git log --oneline - This is kinda self-explanatory.
git log --decorate - Show branches
git log ..branch & git log master.. - These commands can help you find the differences between branches.
17 Feb 2015
Say you’re in Rails and want check that a method exists & that it is not nil:
One way you could do this is:
if my_object
if my_object.my_method
my_object.my_method
end
end
If you were cleverer, you could just do:
if my_object && my_object.my_method
my_object.my_method
...
The trouble with these approaches is they can become cumbersome:
if my_object &&
my_object.my_method &&
my_object.my_method.my_second_method
my_object.my_method.my_second_method
And that’s using the more concise syntax…
A better approach would be to use the Object#try method.
It ‘tries’ an object’s method and, instead of going mental,
calmly returns nil if that method doesn’t work.
my_object.try(:my_method)
Ta da!
This makes the more nested existence-validations read much better:
my_object.try(:my_method).try(:my_second_method)
If you’re using standard ruby, the #defined? and #nil? methods could help…
15 Feb 2015
:h holy-grail
Type it in. Now.
_________
|o^o^o^o^o|
{ _!_ }
\ ! /
`. .'
)=(
( + )
) (
.--' `--.
`---------'
27 Jan 2015
| Shortcut |
Action |
| ggg?G |
rot13 entire buffer |
| guu |
lowercase line |
| gUU |
uppercase line |
| ga |
show hex & ascii value of character |
05 Jan 2015
Vim Registers are where yanked / deleted text ends up.
Registers are usually accessed by the " key.
There are many different registers:
| Register |
Description |
Example |
| Unnamed |
default / automatic |
dw |
| Numbered (0) |
last yanked text |
“0p |
| Numbered (1-9) |
historically deleted text |
“5p |
| Black Hole |
/dev/null of registers |
“_dw |
| Named |
a-z registers (like macros) |
“ay |
Hope this makes sense! To set & get the contents of the register, you just need to do reference the name. Eg: "ayw => "ap
14 Dec 2014
If you want to suspend vim (or many other unix appications), pressing ctrl-z
will ‘suspend’ them.
You can then do important things on your shell.
To return, just type fg (foreground).
If you want to see what else you’ve suspended, jobs will tell you.
23 Nov 2014
If you want to return a true or false value in ruby, you could try something like:
if variable
return true
else
return false
end
That’s not very good. In Ruby, we don’t need to explicitly return variables:
if variable
true
else
false
end
This is a bit long-winded. We could try the ternary operator:
We can thin this out even more by using a double negative:
This is performing an ‘inverse’ !variable method (which returns the opposite bool type).
23 Oct 2014
I decided to make a Twitter Bot hosted on Heroku. And I managed to do it really easily. Surprisingly.
The source code for this is on Github.
Here are the basic steps I took:
1. Think of something funny / useful for your bot to do.
- This will have the ability to control your twitter account on your behalf.
- Make a note of the API keys.
4. Enable Read / Write Permissions for this App
- Found under ‘Permissions’.
- If you need an extra mobile number, try this trick.
- Sferik’s twitter gem makes this very easy in Ruby.
- If you’re looking for inspiration, browse my code. It’s not very complicated… And the twitter gem is very well documented.
- Remember to use Environmental Variables to hide your API keys.
6. Create a Gemfile / Rakefile for Heroku to latch onto.
- Again, examples of these can be found on my Github profile.
- The rakefile basically defines an isolated process that can be run by heroku.
- The Gemfile lists all the Gems you’ll need to install.
- You may need to run
bundle install (as usual…)
7. Check you can do what you want from the command line.
- By running your rake task.
9. Push your current files to Heroku.
- This should be a .rb file, a Rakefile, a Gemfile & a Gemfile.lock
heroku addons:add scheduler - the fastest way to add this.
- This will allow you to customise how often it runs.
- You can set it to run every 10 minutes if you feel the need…
- To customise this, you’ll need to log into you Heroku Account.
11. Add your Environmental Variables to Heroku.
You should be ready to take Twitter by storm now!
22 Oct 2014
I recently created a new twitter account (Damn Nature, You Scary). I had trouble registering a new app as, apparently, each app needs a (unique) mobile number.
The process wouldn’t let me change the permissions of the new app to read and write.
I managed to get this working by:
-
Download the app on my mobile (iOS in my case, although Android apparently works, too)
-
Log into the app
-
Still on your mobile, go to http://dev.twitter.com/apps
-
Change the permissions of your app to Read & Write
-
Click Update
That should work!
20 Oct 2014
Learning a few UNIX commands can be useful. Since it’s the ‘default’ programming language that pops up when you run the Terminal, it’s worth knowing a few of the more basic commands:
These are sorted by ‘difficulty’ - you should know the first ones first.
Navigation
cd - changes the current directory.
cd ~ - changes where you are to HOME directory
cd Documents - goes one file deeper into the ‘Documents’ folder
cd .. - goes up one directory
ls - list files or directories in current directory
ls -la - uses the options ‘-l’ (list in detail) and ‘-a’ (show hidden files)
pwd - print working directory. Shows where you are (relative to the root folder)
Copying
cp - copy
cp anchovy.txt fishes/anchovy.txt - creates a copy of the file under the ‘fishes’ directory.
cp -r - copy recursively. Useful for directories.
cp -r anchovy fishes/anchovy - copies all the anchovy-related files from the folder ‘anchovy’
Deleting
rm - remove
rm platypus.txt - deletes the specified file
rmdir - remove directory
Moving / Renaming
mv - move
- This works like cp, except it removes the initial file / directory
mv anchovy.txt fishes/anchovy.txt - moves & removes the file.
- Renaming:
mv anchovy.txt sardine.txt
Creating Folders
mkdir - creates a new directory
mkdir -p - creates a series of new directories.
mkdir -p Pelecanidae/Pelicans/Great-White-Pelican
Getting help
man - find the manual
Showing files
cat - show pure text version of the file.
- Concatenates & prints the file
cat gerbil_name.txt - simply outputs text: ‘Fred’
less - outputs text with more features.
- Better for larger files. Includes scrolling, searching, etc..
less encyclopedia.txt - won’t crash & is still useful
sort - guess what this one does…
- Go on. Guess. I’m not going to tell you.
Comparing files
diff - shows the differences between two files
diff red_spotted_woodpecker.txt lesser_woodpecker.txt
Finding programs
whereis
- Useful if you’re running the wrong version of ruby. Or something similar.
- Provides the location of the executable file.
Changing File Permissions
chmod
- A common chmod file permission to set is
chmod 755, which is rwx for the Owner but only rw for other users.
Search for text
grep
I’ve written a quick primer on UNIX file permissions here
Grep’s a fairly big topic:

grep "some string" filename
grep "REGEX" filename
grep -i "some string" filename - case insensitive
Flags
Flags are optional parameters that you can pass in to a shell command. We’ve met some already; here’re two: ls -la. You can see a full list of available options under the man page of the commands.