I recently started using Tmux, a great little command line tool for managing multiple terminal windows and stuff.

Sadly, Tmux and MacVim don't get along very well, and copy/paste won't work when you launch MacVim from the command-line (which I do very often).

Luckily, there's an extremely easy solution to this. Just run:

$ brew install reattach-to-user-namespace

And add this to ~/.tmux.conf:

set-option -g default-command 'reattach-to-user-namespace -l zsh'

Finally, if you haven't got it in your ~/.bashrc/~/.zshrc yet, you just run:

tmux source-file ~/.tmux.conf

To load all your config file changes.

Boom! Copy/paste works fine, and you can use Tmux + MacVim for an unbeatable duo of development awesomeness!

Mar 3, 2013

A Little Bit of C

No matter who you are, C is a valuable programming language to know. Common programs we use such as Ruby, Bash, and almost every OS uses C (and maybe some Assembler) at its core!

I decided to compose this blog post for the people who don't know C, or any compiled language for that matter, because C is highly underrated over its cousins C++ and Objective-C. C is messy once you get a large code base, but C can be clean too, that's the programmers job.

Why should I learn C?

  • C is extremely fast
  • C is used to build programming languages
  • C is very useful on the Raspberry Pi, Arduino, etc.
  • C programs can run on almost any OS without modification (unless you use Windows libraries, which I would always avoid unless specifically targeting Windows)
  • Knowing C helps you understand C++ and Objective-C much better
  • Knowing C helps you understand any language better, because you know what it's doing at its core
  • C is used to write native extensions for Ruby, and other language (faster code written in C but creates functions in the language)
  • C is used to program robots, LED screens, and other hardware that we use in our everyday life
  • Since C was made almost 50 years ago, it has all the bugs worked out, and thousands of libraries and tools

Convinced? Let's make a little C program!

Writing Your First C Program

C programs usually end with the .c extension, and have a header file with the same name but it ends with .h. We'll get to header files later on, because first you need to understand the basics of C.

Go ahead and create a file called playground.c. playground isn't a bad name considering it is where we fool around with various C code.

Open playground.c in your favorite text editor, and type the following code into the file:

// This is a comment, you obviously don't
// need to type these in, as they just explain what our
// code is doing.

#include <stdio.h>

int main() {
    printf("I'm a C program!\n");
    return 0;
}

Now that you've written the program, run gcc playground.c -o playground in your command line. You'll get an executable file called playground. Run the executable using ./playground. At last, you should see the following appear in your command line:

I'm a C program!

NOTE: if gcc doesn't work, see the section below Compiling a C Program.

Congratulations! You've successfully written your first C program!

What is Our Code Doing?

C has something called a flag. Flags always start with # with some sort of word following it. Generally flags are used to include other C files or define macros (we won't do macros in this tutorial because they're a fairly complex subject).

We're beginning the file by using the #include flag to import some C code called stdio.h. stdio.h is a built-in C library on every OS, and contains basic input/output functions like puts and printf.

Next in line, we're creating a function called main(). Every C program has a function called main() that does everything in the C program. You can define custom functions, just like every other language, but C needs to find a function called main() to work. Before main() we have the word int, since main() returns the integer zero. Since zero is false, it returns false, which basically returns nothing without the use of void (which I'll explain in the next tutorial).

In the main function, we call a function called printf(). printf() is a basic function that prints a format to the screen, which is a string that can be interpolated (have dynamic values added to it).

Finally, we return the value zero since we declared main() would return an integer.

Compiling a C Program

We compiled our C program using an open source C compiler called gcc. If it doesn't work, try the following:

  • For Mac users, try installing the Xcode command line tools (go to Xcode > Preferences > Downloads > Click "install" next to the "command line tools" column)
  • For Windows users I'd recommend getting the Visual C++ tools or something like MinGW (I'm Unix for life)
  • For Linux try running apt-get install gcc, or search around for other C compilers

If it did work, you basically just compiled playground.c and spit out the binary output to a file called playground, which you can run via the command line.

Every C program that has a main() function can be compiled to a pure binary file (which is really fast), and have its output spit to a specified file.

Tip: gcc's -o option doesn't have to be included, by default the output will be in a file called a.out. Some people end their C output with .o, but it's completely optional and isn't commonly used for a project's main executable.

Let's Make Another C Program

Now that you completely understand how to compile, run, and write some very basic C code, let's do another more complex program. Delete the playground executable, and type the following in (you can delete the comments in the actual file):

// Let's include some more C code to use
// in our program this time... all the functions in
// string.h!
#include <string.h>
#include <stdio.h>

// Here we'll define a custom function that gets the length
// of the string "hello"
//
// we return an integer with the number of characters in the
// string "hello"
int hello_length() {
    return strlen("hello"); // strlen() might look a bit weird, but every C function has shortened words
                                           // note that you need to include string.h to use the strlen() function
}

int main() {
    // we use %i to add an integer to the string, then
        // use a comma to specify that the return value
        // of hello_length() is the integer we want to include
        // in the string
    printf("Hello has %i characters in it.", hello_length());
    return 0;
}

Compile playground.c again, and run the playground executable. You should get the following output as expected:

Hello has 5 characters in it.

Congratulations again! You defined your own function that returns the length of "hello".

Til' Next Tutorial

I can't write everything, so for now this is the end of part 1. Part 2 should be out soon showing you how to define your own return types, use types other than int, define structs, and more awesome stuff.

Thanks for reading, and don't forget to research a lot!

Feb 23, 2013

Objective-C for General Development

Lately in my spare time I've been messing around with lexers, parsers, compilers, VMs, and other topics related to language implementation. I've been messing around with C and C++ a bit, but I will proudly say that Objective-C is the best language.

Objective-C isn't just nicer to write, but it's elegant, fast, and works with C perfectly (not to mention it's a subset of C essentially). As Apple does almost too well, Objective-C is extremely organized as well. Classes and methods always read nice, and make sense, even when writing in a low level language. For an example, [@"hello" length] looks much less complex than strlen("hello"). C++ and C frightens aspiring developers away, while Objective-C invites.

While I was thinking about all of this matter, I realized that Objective-C should be used for all development purposes, not just iOS. Using GNUStep (GNU's open Objective-C compiler), the Objective-C compiler can be installed anywhere and used for many projects. Windows and Ubuntu/Linux among many others support scripting with Objective-C. GNUStep is even compatable with Raspberry-Pi (and possibly Arduino, though I haven't tested it yet)! GNUStep includes all the NS classes, so the full Objective-C language is available for general scripting!

I've now decided that though C and C++ are very general, as well as nice to work with, Objective-C will now be one of my main scripting languages for low-level, fast code.

I don't know if I'm just crazy, but GNUStep is in many ways easier to set up and use than rvm (for setting up the Ruby scripting language).

In conclusion, I think Objective-C should be much more popular for general scripting, rather than C or C++ (specifically in language interpreters and such). If you're interested in compiling Objective-C using GNUStep along with gcc or clang, get googling! I've found tons of helpful tutorials, including Windows ones. Hopefully I've convinced a couple people to give general Objective-C a try.

Again, these are just my thoughts, but I think Objective-C is a truly valid language.

Feb 22, 2013

Using Octokit to Fetch the Latest Commit, and More Tips for Working with the Github API

The Github API is an extremely useful, and nice API for developers. Using the Octokit gem for Ruby, you can easily make great projects fetching data from Github.

How to use the Github API summed up in one example

Octokit is a great little Ruby gem, but I notice fetching some different types of information tends to be difficult at times.

I decided to make a little IRC bot in Ruby, and decided I wanted to make a command to fetch the project's Github repo's latest commit number and message. I thought it would take 5 minutes to figure it out, turnes out, it took about an hour.

It took a while due to some wierd quirks in the data returned, but it turns out it's super-simple.

First, you're going to want to add Octokit to a Gemfile, or just install it:

# gemfiles
gem 'octokit'

# or just install it
$ gem install octokit

Once you've installed Octokit, you're probably going to want to make a simple method that makes the task easier:

def fetch_latest_commit(repo)
    return Octokit.commits(repo).first
end

Basically, when we call Octokit.commits(repo).first, you're simply returning an array of hashes, with sub-hashes. It's a little complex if you're just making a little snippet of code for your site or whatever.

To understand what commits() is returning, go into irb or pry, and type in the following:

require 'octokit'
commit = Octokit.commits('your_name/your_repo').first
puts commit

You'll get a pretty freakin' big hash containing all the info of the latest commit to your_name/your_repo, but if you look closely you'll see hashes in the hash such as commit. Using the properties in these hashes you can finally get some info on the latest commit. Now you can add this to your file using your fetch_latest_commit() function:

commit = fetch_latest_commit("your_name/your_repo")
puts commit[:commit][:message] # get the hash "commit" and return the "message" property

Now, if you commit to your repo:

$ git commit -m "Added file"

You can push it up to Github, and run your script. You'll output will be:

Added file

Congrats! You can now get the lastest commit description!

Other stuff to know

Don't forget, almost all the API methods return a hash like the one in IRB you just returned. You can modify your function to get a list of latest commits, different information on the commits, and so on.

You can use functions like commits_since() and commits_on() to fetch different commits, and read from the different sub-hashes in commits to fetch all sorts of info. A helpful way to read the properties of sub-hashes (that's not big, and you can actually read) is to type puts commit[:commit]. It will return all the properties in the sub-hash :commit only, instead of returning all the sub-hashes with all their properties.

Let's use the Github API to fetch the two latest commits on a repo, and spit out their description, commit number, and author's name:

require 'octokit'

def commit_info(cnum, cdesc, cauth)
    return "#{cnum} - \"#{cdesc}\" by #{cauth}"
end

def fetch_commits(repo)
    commit_one = Octokit.commits(repo).first # latest commit
    commit_two = Octokit.commits(repo)[1] # commit before the latest

    # The commit number (sha) isn't in a sub-hash, but the commit author
    # is returned by getting a property in a sub-hash of a sub-hash!
    # Pretty complex, but if you break each API hash down, you'll get
    # just what you need.
    commit_info(
        commit_one[:sha],
        commit_one[:commit][:mesage],
        commit_one[:commit][:author][:name], # to get the author's name, you get a property from a sub-hash called 'author', in a sub-hash called 'commit'
    )

    # same goes for commit two
    commit_info(
        commit_two[:sha],
        commit_two[:commit][:message]
        commit_two[:commit][:author][:name]
    )
end

# finally, use the fetch_commit() function to fetch two of
# the latest commits in your given repo argument, and return
# the commit number, description, and author
fetch_commit("your_name/your_repo")

Finally, you can make some changes to your repo and commit again:

$ git commit -m "Change file"

Now run your script, and you should see something like the following:

1c331f62686a605459472a5390c69db27edf7256 - "Add file" by Yourname
1c331f62686a605459472a5390c69db27edf7256" - "Change file" by Yourname

Resources


Thank you very much for reading, and please email any bugs or comments you want me to know about to chris@seven7.ca.

Jan 17, 2013