Project 1

Getting Started with Ruby

Computers are almost everywhere today — from laptops, tablets, or phones, to TVs, watches, medical devices, kitchen appliances, cars, spaceships, big factories, little robots, and millions of other places large and small.

How do computers know what to do inside all these things? Someone has to teach them! Behind every cool animated movie, website, game, vehicle, or device, someone has worked hard to instruct a computer on how to perform its task. That person was a programmer.

In this chapter, I give you a little background about programming and how programmers organize their thoughts when writing computer software or code. I share some background about Ruby, the programming language I cover throughout this book. Then I tell you how to install the tools you’ll use for all the projects in the rest of the book.

image

What Is Programming?

Computers are kind of dumb by themselves. Without a person to tell it exactly what to do, a computer will just sit there. Everything a computer does — and I mean everything, from the display of pictures and text on a screen, to the understanding of what you type on a keyboard or touch and swipe on a tablet — requires some software to interpret signals coming through the various circuits in one part of the computer and modify and send them to the right place in another part to get something done. That’s a lot of work!

Fortunately, over the years, many smart people have come up with different ways to communicate clearly with computers. Writing instructions for a computer is called programming or coding, and the end result is a program or software.

A computer programming language shares many similarities to a human language. It has symbols and words (like nouns and verbs) that you put together following a syntax (rules for spelling, order, and punctuation).

When you start learning to program, you open up a wide world in which you can apply this knowledge when working with any technology that uses computers. You’ll be able to read other people’s programs to learn more about computers or to use code you write to solve homework problems, create puzzles, build a new game, create a website, or even control machines like robots.

Programs needs to be very precise in order to instruct a computer to do something. Imagine that you want to tell your friend to do something. For instance, how would you tell someone to sit down in a desk chair? You might say:

  1. Pull the chair out.
  2. Sit down.

Your friend is smart enough that your instructions make perfect sense, and she’ll sit on the chair safely without falling over or anything crazy like that. People have a lot of knowledge they can use to interpret instructions like this.

Now, if you have to tell a computer to sit down, what would that be like? You have to be a lot more exact. For example, you would have to say:

  1. Pull the chair away from the desk.
  2. Walk around so your body is in front of the chair.
  3. Turn around so your backside is facing the chair.
  4. Make sure your body is exactly next to the chair.
  5. Start bending your knees and lowering your body.
  6. Keep bending your knees until your bottom makes contact with the seat of the chair.
  7. Stop bending your knees when your weight is held by the chair.

Even these instructions might not be enough for a computer because they make some assumptions (like what your body parts are called).

Try it yourself: How would you tell a computer exactly how to do something like filling a glass with water?

Programmers need to think in this very detail-oriented way. As you learn to write computer programs, you’ll get good at breaking a problem down into smaller and smaller parts. Each of those parts will eventually be a line of code that you create. Over time, you’ll learn other techniques that help you identify the different objects you’ll need to describe to the computer and the actions those objects will take. This will help you organize your code in ways that make it possible to create very sophisticated software. Pretty cool, huh?

Why Ruby?

There are many different computer programming languages out there. Each language has strengths and weaknesses. Some languages are easier if you’re trying to control large machines. Some languages are specialized for mobile apps — the kind on an iPhone, for example. Some languages make it easy to create websites. And some languages are for doing science and engineering.

A general-purpose programming language is good for many different kinds of projects. There are many general-purpose programming languages to choose from. The important thing when you’re wanting to learn programming is to pick something and dive into training yourself to think like a programmer. When you learn one programming language, learning another one is much, much easier.

In this book, I use the language Ruby. Ruby is a flexible, general-purpose language that is useful for many kinds of projects. It was created in the mid-1990s in Japan by Yukihiro Matsumoto (best known by his nickname, “Matz”). Don’t worry — you don’t have to learn Japanese to program with Ruby! Today Ruby is used around the world for all kinds of projects, by beginners and professionals alike.

Matz had a wonderful philosophy in mind when creating Ruby: He wanted programmers to be productive, enjoy programming, and be happy. This is one of my favorite things about Ruby: As you learn it and write programs, you’ll have fun!

What Tools Do You Need?

Most obviously, you need a computer that’s running a current version of a consumer desktop operating system (Mac OS X or Windows).

technicalstuff If you’re using a computer with Linux on it, you can still follow along with the projects in this book. I won’t be going through the instructions here. Instead, check out the official Ruby documentation: www.ruby-lang.org/en/documentation/installation. As long as your selected approach installs at least version 1.9.3 of Ruby, you should be okay.

For the projects in this book, you need only a few basic tools, and they’re all free.

First, you need Ruby installed, as well as some other software that helps Ruby use the capabilities of your computer. I walk you through how to install Ruby in this section.

Second, you need a text editor that is specifically for coding. Word processors don’t work well when coding, so you’ll use a tool that is built for programmers. There are a number of good, free code editors out there, and I help you install one of them in this section. (You may use any other editing program you like as long as it’s a code editor of some kind.)

If you’re on Windows

To run Ruby on Windows, you have to install Ruby and several developer tools. The following instructions have been tested with Windows 8 and 8.1.

  1. Go to http://rubyinstaller.org in your web browser.
  2. Click the big red Download button.

    A list of RubyInstallers appears.

  3. Click Ruby 2.2.2 near the top of the RubyInstallers list (see Figure 1-1).

    warning Do not click Ruby 2.2.2 (x64).

    An installer program downloads to your computer.

  4. Run the installer program by choosing Run Program (if Windows presents this option) or double-clicking the file when it’s done downloading.

    The installer will ask you to select a language to use during installation. Accept the license, and then the installer will have you set some configuration options. Leave the default folder choice alone, but uncheck the Install Tcl/Tk Support check box (you won’t be using it for this book), and make sure that the other two check boxes — Add Ruby Executables to Your PATH and Associate .rb and .rbw Files with This Ruby Installation — are selected (see Figure 1-2).

    When the installer is done, it will have created a topmost folder with all the Ruby software on your C: drive called C:Ruby22. You can use Windows 8 Desktop and the File Explorer to confirm that it’s there (as shown in Figure 1-3).

image

Figure 1-1: Click Ruby 2.2.2 to download installer.

image

Figure 1-2: Setup Ruby installation settings.

image

Figure 1-3: Confirm that the Ruby22 folder is created.

You must also download the Development Kit from http://rubyinstaller.org to get some of the cool tools used by the projects in this book. Follow these steps:

  1. Go to the http://rubyinstaller.org in your web browser.
  2. Scroll down to the Development Kit section and click the file under “For use with Ruby 2.0 and above (32bits version only)” (see Figure 1-4).

    An installer program downloads to your computer.

  3. Run the Development Kit installer by choosing Run Program (if Windows presents this option) or double-clicking the file when it finishes downloading.

    The installer will ask you where to put the kit. You want to put the kit in its own folder, not in the Ruby folder you selected in Step 1. To make things easy for the projects in this book, select a folder next to the Ruby22 one at the top of your C: drive.

  4. Enter C:DevKit for the location, as shown in Figure 1-5.
image

Figure 1-4: Download the Development Kit for Ruby 2.0 and above. Be sure to click the 32bits version.

image

Figure 1-5: Select the DevKit installation directory.

Now you need some final setup. Follow these steps:

  1. Open your Windows launch screen (or use the Start Menu).
  2. Click the Start Command Prompt with Ruby program (my machine looks like Figure 1-6).

    tip If you have a lot of programs installed in your launch screen, use the search feature and type command to help narrow down the choices.

    In the command prompt application, you type commands to get things done. Typing commands is a low-level way of working with a computer. Before the mouse and graphical interface was invented, this was the only way to tell the computer what to do!

  3. Change your location to the Developer Kit folder.

    When you first open the command prompt application, you’re usually in your home directory. To complete setup, you need to move to the DevKit directory. The display will show you a prompt that gives a hint of where you’re starting from:

    C:Userschris>

    When you see commands in this book, you see them displayed next to the prompt. You don’t need to type the prompt part, just the command line itself.

    Change folders by typing cd DevKit and pressing the Enter key to tell the computer you’re done with that command.

    You see the prompt change to your new location:

    C:Userschris > cd DevKit

    C:DevKit>

  4. Use Ruby to set up more Ruby tools.

    Development Kit has a Ruby setup program called dk.rb that you use for a couple of different steps. Enter the first command and watch for it to complete, as shown in Figure 1-7.

    C:DevKit> ruby dk.rb init

    If you see the Initialization complete! message, Development Kit is ready to finish installing its tools.

  5. Enter the install command and wait for it to complete (see Figure 1-8):

    C:DevKit> ruby dk.rb install

    Phew! You only have to do these commands once to get your computer ready for Ruby programming and you’re almost there!

  6. The Development Kit is ready and now you can install Ruby gems (little add-on enhancements for Ruby) that you use for the more advanced projects in this book.

    You learn a lot more about Gosu, the graphics and game programming library in later chapters. Enter the command and review the progress messages (see Figure 1-9):

    C:DevKit> gem install gosu

    warning You may get a Windows security warning saying that Ruby is trying to use the network. This is okay — you can select the default values in the dialog box. You may have to enter your password to dismiss the dialog box.

image

Figure 1-6: Click Start Command Prompt with Ruby.

image

Figure 1-7: Initialize the Development Kit.

image

Figure 1-8: Install the Development Kit.

image

Figure 1-9: Install the Gosu gem.

Wow, that was a lot, but now you have Ruby installed!

Now you will want a code editor to help make writing your programs easy:

  1. In your browser, go to www.atom.io and click the Download Windows Installer button (see Figure 1-10).

    Atom is a free, powerful code editor that can be used for programming many different computer languages. It works well with Ruby.

    An installer program downloads to your computer.

  2. Run the Atom installer by choosing Run Program (if Windows presents this option) or double-clicking the installer program when it finishes downloading.

    You see a progress dialog box. When the installer is done, the Atom editor starts up (see Figure 1-11).

    You see the Atom welcome screen, which means you’re all set to start programming with Ruby!

image

Figure 1-10: Download the Atom Editor.

image

Figure 1-11: The Atom welcome screen.

technicalstuff Ruby is constantly being updated and improved. The projects in this book use the current release of Ruby at the time of the book’s writing, version 2.2.2. Ruby uses a versioning scheme where the first number is the major version, the second number is the minor version, and the third number is the current build number (sometimes called the patch number). Most everything in this book should work with versions of Ruby all the way back to version 1.9.3, but it would be best if you use version 2.0.0 or better. On Windows, to utilize the game programming library Gosu, it’s also important to select the version of Ruby and the Development Kit that indicate they are 32 bit.

If you’re on Mac OS X

tip To install the needed developer tools for Mac OS X, you need to be logged in to your computer using an account with Admin access. If you’re the only person using the computer, you’re usually an administrator by default. If you share a computer at home, school, or work, you may need help to get access to an administrative account. You can check your access level by going to the Users & Groups control panel inside the System Preferences application (my machine looks like Figure 1-12). You only need Admin access during setup, not while completing the projects in this book.

image

Figure 1-12: Confirm that your account is an admin in Users & Groups.

To run Ruby on Mac OS X, you have to install Ruby and several developer tools. The following instructions have been tested on Mac OS X Yosemite (10.10.4). They should work fine with Mac OS X El Capitan (10.11.1) or Mavericks (10.9.5) as long as you’re running the latest updates for that version of Mac OS X:

  1. Open the Applications folder, and then open the Utilities folder (see Figure 1-13).
  2. Open the terminal application.

    You see a prompt indicator that looks like a dollar sign ($) (see Figure 1-14). In the terminal application, you type commands and press the Return key to get things done. Typing commands is a low-level way of working with a computer. Before the mouse and graphical interface was invented, this was the only way to tell the computer what to do!

    Note that your default prompt will include information like the name of your computer, the directory you’re in, and even who you’re logged in as:

    Christophers-MacBook-Pro:~ chaupt$

    tip In this book, I omit the full prompt and just show the $ to save space.

  3. Mac OS X comes with a version of Ruby preinstalled; check the version by typing ruby --version at the command prompt:

    $ ruby --version

    ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]

    In this example, the version of Ruby is 2.0.0, and the number following the letter p is the current patch or build number. On my machine, the patch level is 481. Although newer versions of Ruby are available, and the Mac’s version will change if you install updates, the current version should work great for the projects in this book.

  4. To use some of the Ruby gems (little add-on enhancements for Ruby) needed for the projects in this book, you must install Apple’s command-line programming tools. These tools are a part of the free Xcode development tools package that Apple makes available. In the terminal, enter the following command:

    $ xcode-select --install

    After you press Return, the program will open a window to confirm you want to install the tools (see Figure 1-15).

  5. Click the Install button, agree to the license, and wait for the tools to get set up.

    This step will take a few minutes depending on the speed of your Internet connection.

  6. Next, you install a set of software installation tools called Homebrew. Homebrew makes it easy to install and update additional software, called packages. Some of the projects in this book use Ruby gems that rely on low-level software to get their jobs done. Homebrew makes it much easier to get everything working. In your browser, go to the Homebrew web site, www.brew.sh (see Figure 1-16).

    You see instructions for installing Homebrew. The page shows a really long command that starts with ruby -e. You can copy it from the web page and paste it into your terminal window at the prompt.

    remember This is one long line, so if something goes wrong, make sure that you got the whole thing.

    The command uses Ruby to install the Homebrew tool. It displays a warning about using sudo and then asks you for your password to finish the installation. This warning may be unfamiliar, but it’s safe to proceed with the installation. Many progress messages will fly by on your screen (see Figure 1-17). If you get a message saying that you haven’t agreed to the Xcode license, follow the instructions that are displayed and continue on here.

    technicalstuff If you’re just learning about command-line programs on Mac OS X, you may be unfamiliar with using sudo. sudo is a way to give temporary permission to a program to use Administrator privileges. Homebrew needs this permission to set up the directories and software needed to do its job. The Homebrew installation program is used by thousands of people and is very careful about where it puts its software. If you ever need to use sudo yourself, you want to be extra careful that you type the associated commands correctly.

  7. Once Homebrew installation is done, you can check that all is okay by typing the following command:

    $ brew doctor

    If everything is set up properly, you should see a message saying Your system is ready to brew. Otherwise, you may have some instructions to follow to finish updating Homebrew.

  8. Homebrew’s purpose is to make installing low-level software easier. Now, install some of the libraries of code you need for future projects:

    $ brew install sdl2 libogg libvorbis

    After you press Return, you see a series of progress reports as Homebrew installs the software (see Figure 1-18).

  9. Now you can install the Gosu gem that you use to build the projects later in this book. Enter the following command:

    $ sudo gem install gosu

    Ruby installs the gem and should provide a confirmation message (as shown in Figure 1-19).

    You need to use sudo here because when you install a Ruby gem, you install it for the entire system to use. Remember to take care when entering the command — you’re giving it special permissions using the sudo program.

image

Figure 1-13: Locate the terminal program in the Utilities folder.

image

Figure 1-14: The terminal application with a standard prompt.

image

Figure 1-15: The xcode-select confirmation dialog box.

image

Figure 1-16: The Homebrew home page and installation command.

image

Figure 1-17: Homebrew successfully completes installation.

image

Figure 1-18: Homebrew installs the necessary libraries.

image

Figure 1-19: Gem installation completes successfully.

You have Ruby and its associated developer software ready to go, so now you need a programming editor:

  1. In your browser, go to www.atom.io.
  2. Click the Download for Mac button (see Figure 1-20).

    Atom is a free, powerful code editor that can be used for programming many different computer languages. It works well with Ruby.

    Depending on the settings of your browser, the Atom download archive may automatically uncompress or a zip file may be placed in your Downloads folder.

  3. Drag the Atom icon to your Applications folder.

    If you see a zip file rather than the Atom icon, double-click the zip file to uncompress it manually.

  4. Double-click the Atom icon in the Applications folder and check out the editor.

    You should see the Atom welcome screen (shown in Figure 1-21). You’re all set to start programming with Ruby!

image

Figure 1-20: The Atom editor for Macintosh home page.

image

Figure 1-21: The Atom editor for Macintosh welcome screen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.119.114