Setting up Ruby on Rails with MongoDB on a Windows machine.

You need to have cygwin installed with ruby and git packages (obviously you may want to have more).

The following commands are executed in the cygwin prompt:

git clone git://github.com/rubygems/rubygems.git
cd rubygems/
ruby setup.rb
gem install rails

Go to the MongoDB website and download Windows binaries: http://www.mongodb.org/downloads. Extract the content of the bin/ directory to C:\cygwin\usr\local\bin.

Create a directory for the db files (the default MongoDB db files directory is C:\datadb):

cd /cygdrive/c
mkdir data
mkdir data/db

Done! Both mongo and rails are in your cygwin’s path now, feel free to tweak it as you see fit.

I’ve been giving preference to a keyboard over mouse since I discovered vim for myself, as it’s a faster and more convenient way to go. I am a Chrome user and recently I found an amazing plugin: Vimium.

It does exactly what the name suggests, allowing you to use vim-like commands in your browser. You can freely move, switch between tabs, work with forms and click links using familiar vim key bindings.

A two minute long introductory video explains basic commands and you’re all set! I’ve been using Vimium for over a week now, an amusing experience which allows you to throw your mouse in a dark corner (well, not exactly: Vimium still has some issues with over-bloated ajax pages, not to mention Flash and other nasty stuff).

Check it out: http://vimium.github.com/.

Today I had to merge changes from one repository into another. Let’s assume you want to merge beta into alpha.

Operations are performed in repo alpha:

git remote add beta_repo git@rosipov.com:beta.git
git fetch beta_repo
git merge beta_repo/master

In this case, beta_repo is the name you pick for remote.

If you just need to cherry-pick a certain commit from beta you can omit the last step and replace it with the cherry-pick.

More on the topic of remotes: http://git-scm.com/book/ch2-5.html.

To encrypt and decrypt files in Linux there is a utility called gpg (Gnu Privacy Guard). This is a short GPG tutorial.

Quick usage example

gpg -c foo.txt

It will prompt you for the passphrase and a confirmation. Now you will have the encrypted foo.txt.gpg file. To decrypt a file:

gpg -d foo.txt.gpg

This will forward the output to the console. You can output it into a file:

gpg -d foo.txt.gpg > foo.txt

GPG keyring

This is all secure, but not quite enough if you are paranoid. Keys are what makes gpg great. Let’s generate a private key:

gpg --gen-key

And create an ASCII version of a public key:

gpg --armor --export "John Doe" --output johndoe.txt

Public key johndoe.txt can be freely distributed. Now you can encrypt files for yourself only:

gpg -e -r "John Doe" foo.txt

Now if you decrypt a file it will require the passphrase you specified while generating a key. To encrypt a file for someone else you should have this person’s public key.

Let’s assume Stan Smith sent you a key, stansmith.txt. You import it using:

gpg --import stansmith.txt

And encrypt the file:

gpg -e -r "Stan Smith" foo.txt

I had some issues with the strtok function. Here’s a detailed explanation and usage for it.

The strtok function is used to tokenize a string and thus separates it into multiple strings divided by a delimiter.

#include <stdio.h>
#include <string.h>

int main() {
    int j, i = 0;
    char delim[4];
    char user_input[81], *token[80];
    user_input[0] = 0;  // set first byte to 0 to detect empty string

    // validate the string length
    while (strlen(user_input) <= 1 || strlen(user_input) > 82) {
        printf("Feed me a string to tokenize: ");
        fgets(user_input, sizeof(user_input), stdin);
    }

    printf("And a delimiter (up to 4 chars): ");
    fgets(delim, sizeof(delim), stdin);

    token[0] = strtok(user_input, delim);  // first call returns pointer
                                           // to first part of user_input
                                           // separated by delim
    while (token[i] != NULL) {
        i++;
        token[i] = strtok(NULL, delim);  // every call with NULL uses
                                         // saved user_input value and
                                         // returns next substring
    }

    for (j=0; j<=i-1; j++) {
        printf("%sn", token[j]);
    }

    return 0;
}

Let’s compile and execute it:

Feed me a string to tokenize: foo/bar/baz
And a delimiter: /
foo
bar
baz

The first call to strtok returns the pointer to the first substring. All the next calls with the first argument being NULL use the string passed at the first call and return the next substring. The function returns NULL if no more substrings are available.