Git Log

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.

The Object#try method in Rails

Say you’re in Rails and want check that a method exists & that it is not nil:

my_object.my_method

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…

The Holy Grail of Vim Commands

:h holy-grail

Type it in. Now.

     _________
    |o^o^o^o^o|
    {   _!_   }
     \   !   /
      `.   .'
        )=(
       ( + )
        ) (
    .--'   `--.
    `---------'

Random Vim Shortcuts

Shortcut Action
ggg?G rot13 entire buffer
guu lowercase line
gUU uppercase line
ga show hex & ascii value of character

Vim Registers

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

Suspend Vim

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.

Ruby: Return a Boolean

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:

variable ? true : false

We can thin this out even more by using a double negative:

!!variable

This is performing an ‘inverse’ !variable method (which returns the opposite bool type).

Create a Free Twitter Bot - hosted on Heroku

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 is the hard part…

2. Register a new Twitter Account

3. Create a new Twitter ‘app’ - go to http://dev.twitter.com/apps

  • 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.

5. Write some code which interacts with the Twitter API.

  • 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.

8. Create a new Heroku App.

9. Push your current files to Heroku.

  • This should be a .rb file, a Rakefile, a Gemfile & a Gemfile.lock

10. Add Heroku Scheduler to your Heroku app

  • 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!

Twitter Apps Authentication - without another mobile number

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:

  1. Download the app on my mobile (iOS in my case, although Android apparently works, too)

  2. Log into the app

  3. Still on your mobile, go to http://dev.twitter.com/apps

  4. Change the permissions of your app to Read & Write

  5. Click Update

That should work!

UNIX Shell For Dummies

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.

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 pelicans

mkdir -p - creates a series of new directories.

  • mkdir -p Pelecanidae/Pelicans/Great-White-Pelican

Getting help

man - find the manual

  • man man
  • man pwd

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 Book

  • 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.