From Bradwiki
Jump to navigation Jump to search

This is my beginners guide (autobiography) to Ruby programming. It assumes you are somewhat computer savvy but know very little about UNIX and Ruby. Perhaps you've taken an introductory programming course in Java or C++, maybe you've built a website or two, or perchance your research project has you opening the terminal window from time to time. If any of those things are (approximately) true, and/or you have a penchant for computer utility, this guide should be very helpful. In case you haven't gleaned this fact, I don't know how to program using Ruby, and I will be learning and writing this guide at the same time. That's the point. If I can get everything set up on my machine and program something useful using Ruby, there's no reason you can't follow along, share my trials and tribulations, and ultimately learn (I mean really... LEARN) how to program in Ruby. Sound good?

Here's the plan. I am going to use Ruby to do two things. First, I'm going to scrape the content of a website and do something useful with the data, like predict who will win the World Series this year. Second, I'm going to build a blogging or news website that interacts with a database. As I attempt these programming tasks, I'll log my thoughts and experiences in this guide as true-to-life as I can. The only time I will deviate from this concept is if I do something harmful to my computer. I will absolutely make the reader aware of this potential danger, and provide the correct way to complete the task. That said, you are encouraged to follow along using your own computer.

And we're off!

My machine is a Macbook Pro (i7 64 bit running OSX 7 Lion), as such I will be using the UNIX back-end for programming purposes. Yes, your mac is a very expensive UNIX box. That's a good thing. If you have a PC running Microsoft Windows, you have an expensive non-UNIX box, and will need to install some UNIX emulation software like cygwin before jumping into this guide. The rest of this guide will assume you have a Mac, but if you're running cygwin on Windows or are booting into some Linux version like Ubuntu, you should be good to go (and can provide comments where necessary).

If you're on a mac, the first thing you want to do is install xcode developer tools. This will pimp out your mac with a variety of coding tools and programming languages. This is a necessary step. Follow the link provided and download the software from the app store. Install xcode like you would any other program on your mac.

Unix Tidbits

  • Once upon a time Bell Labs (the phone company) released UNIX to universities including Berkeley and MIT
  • Berkeley grad students (like Bill) improved the original UNIX system and released versions called BSD (Berkeley Software Distribution) UNIX. Sun (SunOS or Solaris), HP, IBM, Bell SysIV, Novell and others continued to tinker with BSD.
  • MIT gurus (e.g. Rich Stallman) made a GNU (Gnu's Not Unix) version of UNIX, which is freely available. Free, meaning you can improve it and sell it, but the source code of whatever you sell is to be free to modify, sell, or give away (in turn, that product is also GNU).
  • A shell is a unix program that takes commands at the terminal
  • There are 2 main shell origins, one by a guy named Bourne and another by Bill. Bourne made the Bourne shell (resulting in ksh and bash) and Bill made the C shell (resulting in csh and tcsh)
    • Bourne shell prompts use a $
    • C shell prompts use a %
  • An exclaimaiton point (a BANG -- !) means do a command again, two (!!) mean do the last command again. One followed by a we letters (!cp) means run the last command that started wit h"cp"
  • To stop a UNIX command press ctrl+c
  • How to pronounce Linux: http://wunsite.unc.edu/LDP/links.html
  • GUI means Graphical User Interface while WIMP means Windows, Icons, Mouse Pointing
  • Xerox made the first GUI, MIT made the second called X Windows
  • At the prompt type %xeyes
  • To open a new xterminal type {%xterm}
  • UNIX commands take options (aka flags or switches) which are typed after a command and use a dash like:
    • %ls -l
    • %ls -a
    • %ls -a -l
    • %ls -al
  • Putting a period (.) before a file name will hide the file, but you can see hidden files using the ls command with flags:
    • %ls -al
  • Preview a text file using 'cat' or 'head'
    • %cat filename
    • %head filename
  • Find your user rights (permissions) using the 'id' command:
    • %id
  • Find the permissions of a file using ls with flags
    • %ls -l filename
  • The permissions of a file will look something like this:
    • -rw-r-r-
      • the first dash will tell you it's a file (if it's a 'd' then it's a directory)
      • the first set of letters will tell you the owners permissions
      • the second set of letters will tell you the permissions of people in the same group as the owner
      • the third set of letters will tell you what anyone can do with the file
  • You can change a file's permissions using '%chmod'
  • Everyone gets a home directory, yours probably looks like this:
    • /usr/yourname/
  • The command '%pwd' will tell you your current directory
  • You can go to, or reference your home directory using a tilde (~)
  • A dot (.) references your current folder, two dots (..) references the parent folder
  • To see files in the parent folder (aka parent directory) type:
    • %ls ..
  • Here are some popular directories:
    • /bin/ system commands
    • /bin/usr/ more system commands
    • /user/local/bin/ non-standard add-on commands
    • /dev/


After several moments of deliberation, I chose to create the page Unix as a starting point for this beginners guide to programming.