20,99 €
The fun way to introduce coding with Ruby to kids If you don't have the chance to take coding classes at school or in camp--or if you just want to learn on your own--Ruby For Kids gears you up to expand your technology skills and learn this popular programming language. Written in a way that's easy to follow--and keeping the super tech-heavy stuff to a minimum--it quickly and easily shows you how to use Ruby to create web and mobile applications with no experience required. Ruby is considered one of the best and simplest languages to start with when you're learning coding. This fun and friendly guide makes it even easier. Broken down into simple projects designed to appeal to younger programmers, Ruby For Kids gets you up and running with core coding concepts in no time. Before you know it, you'll be tackling hands-on projects, enjoying the support of a vibrant community, and feeling a sense of accomplishment as you complete projects. * Navigate the basics of coding with the Ruby language * Use Ruby to create your own applications and games * Find help from other Ruby users * Offers tips for parents and teachers helping kids learn Ruby So what are you waiting for? Ruby For Kids has everything you need to get in on one of the most popular topics around!
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 293
Ruby For Kids For Dummies®
Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com
Copyright © 2016 by John Wiley & Sons, Inc., Hoboken, New Jersey
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc., and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: WHILE THE PUBLISHER AND AUTHOR HAVE USED THEIR BEST EFFORTS IN PREPARING THIS BOOK, THEY MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS BOOK AND SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES REPRESENTATIVES OR WRITTEN SALES MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR YOUR SITUATION. YOU SHOULD CONSULT WITH A PROFESSIONAL WHERE APPROPRIATE. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM.
For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit www.wiley.com/techsupport.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2015941961
ISBN 978-1-119-05590-7 (pbk); ISBN 978-1-119-05599-0 (ebk); ISBN 978-1-119-05600-3
Table of Contents
Cover
Introduction
About This Book
Foolish Assumptions
Icons Used in This Book
Beyond the Book
Where to Go from Here
Part I: The Most Basic Building Blocks
Project 1: Getting Started with Ruby
What Is Programming?
Why Ruby?
What Tools Do You Need?
Project 2: Big Numbers
Starting Interactive Ruby
Entering Numbers
Doing Some Basic Math
Supersizing the Math with Huge Numbers
Adding Memory by Storing Results in Variables
Using Variables to Repeat a Calculation
Fixing Things When Something Goes Wrong
Trying Some Experiments
Project 3: Bigger Hello World
Starting Interactive Ruby
Knowing How Letters and Words Differ from Numbers
Doing Math with Words
Doing Other Things with Strings
Storing Strings in Variables
Making Some Big Letters
Trying Some Experiments
Part II: Programmers Are Lazy! Stop Typing So Much!
Project 4: Shapes
Organizing a New Project
Printing versus Using puts
Getting Input with gets
Running the Program on the Command Line
Creating Code to Draw a Rectangle
Creating Code to Draw a Triangle
Drawing a House Using Your Two Shapes
Testing Your Program
Trying Some Experiments
Project 5: Simple Adventure
Organizing a New Project
Planning the Project
Looking at the Program Skeleton
Creating the Main Game Loop
Creating Game Rules Methods
Creating Game Helper Methods
Trying Some Experiments
Project 6: Number Guessing
Organizing a New Project
Planning the Project
Looking at the Program Skeleton
Creating Placeholder Classes
Adding Player Methods
Writing the Game Class Code
Trying Some Experiments
Part III: Working with Lots of Your Own Data
Project 7: Short Straw
Organizing a New Project
Planning the Project
Looking at the Program Skeleton
Creating Placeholder Classes
Coding the Straw Methods
Coding the Player Methods
Coding Game Methods
Trying Some Experiments
Project 8: Code Breaker
Organizing a New Project
Planning the Project
Creating Placeholder Classes
Coding CodeBreaker Methods
Coding Caesar Methods
Trying Some Experiments
Project 9: Acey Deucey
Organizing a New Project
Planning the Project
Looking at the Program Skeleton
Creating Classes
Trying Some Experiments
Part IV: Using Shared Code to Get Graphical
Project 10: A-maze-ing
Organizing a New Project
Planning the Project
Looking at the Program Skeleton
Creating Placeholder Classes
Coding Amazing Methods
Coding Game Methods
Coding Level Methods
Coding Tile Methods
Coding Player Methods
Trying Some Experiments
Project 11: Tower
Organizing a New Project
Planning the Project
Looking at the Program Skeleton
Creating Placeholder Classes
Coding Post Methods
Coding Disc Methods
Coding Game Methods
Trying Some Experiments
Project 12: Game of Life
Organizing a New Project
Planning the Project
Looking at the Program Skeleton
Creating Placeholder Classes
Coding Cell Methods
Coding Grid Methods
Coding Game Methods
Trying Some Experiments
About the Author
Cheat Sheet
Advertisement Page
Connect with Dummies
End User License Agreement
Cover
Table of Contents
Begin Reading
iii
iv
vii
viii
ix
x
xi
1
2
3
4
5
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
329
330
331
332
333
334
335
336
337
338
339
340
Ruby For Kids For Dummies is an introduction to the basics of coding using the Ruby programming language. In each chapter, I walk you through a step-by-step set of instructions to create a Ruby program for your Mac or Windows computer. You don’t need to have any programming experience to understand this book, but you do need to have a sense of curiosity and adventure!
The Ruby programming language has been around since the mid-1990s and has become very popular with web application programmers. It can be used for so much more than just web apps. In this book, you’ll see that you can use Ruby for small “command line” tools and calculations; larger programs for home, work, or school; or even graphical games (and I’ll show you a lot of games).
Ruby was designed by its creator Yukihiro Matsumoto to be both fun and productive. My hope is that as you explore the projects in this book, you’ll definitely have fun and be inspired to continue to use Ruby (or any other programming language) to realize your own coding ideas.
Programming in general is similar to sports, music, or even creative arts. It’s hard to just absorb a book on the subject and expect to understand it completely or start to gain mastery of the topic. Instead, you need to have keyboard time and practice. Even professional coders continue to practice throughout their careers.
By exploring and playing around with the projects here, you’ll be taking the first steps down a really interesting Ruby-colored road.
Programming is a large topic, and Ruby itself is a very powerful language. I’ll be working to shed light on some of the more fundamental parts of Ruby and coding in general. There is no rush to finish the projects in the book. Go through each Ruby For Kids For Dummies project as quickly or slowly as you like. Each chapter’s project is a self-contained useful utility or fun game. Along the way, you’ll learn how to use the very same tools that the professionals use, and learn the kinds of techniques that will help you grow as a programmer.
You don’t need to have any previous programming experience, but if you know a little, that’s fine — you’ll pick up how Ruby does things and also see some similarities to other languages. I’ll show you the “Ruby way” when applicable, but I’ll also show the easy way when you’re just learning the concepts.
Topics covered in this book include the following:
The general way to structure simple Ruby programs
Ruby expressions and operators
Organizing functionality using methods and objects
Basic ways to represent data like numbers, strings, and arrays
Using loops
Making choices with
if…else
statements
Learning to program with Ruby isn’t just about writing code in the language. You also need to learn about the tools, resources, and community that stand behind the language.
Ruby has become so popular because it’s a relatively simple language to learn, and the tools needed to write Ruby, test it, and run it are widely available and free. In this book, I help you get started with just a few basic, free, programs that do everything you need to create some pretty sophisticated pieces of software.
You’ll also learn about general programming techniques, and most important, see a wide variety of projects that will pique your interest and hopefully encourage you to take your exploration to the next level.
To make this book easier to read, you’ll want to keep in mind a few tips. First, all Ruby code and all terminal commands appear in monospaced type like this:
puts "hello programs! Welcome to Ruby"
The margins on a book page don’t have the same room as your monitor likely does, so long lines of Ruby and any output it creates may break across multiple lines. Remember that your computer sees such lines as a single line of Ruby. I show that everything should be on one line by breaking it at a punctuation character or space and then indenting any overage, like so:
def room_type
["cave", "treasure room", "rock cavern", "tomb", "guard room", "lair"].sample
end
Ruby is case sensitive, which means that swapping the use of uppercase or lowercase letters or a combination of the two can break things. In order to make sure that you get the correct results from the projects in the book, always stick to the same capitalization and spelling that I use.
Ruby also cares about the kind of quotation marks that you use! So, if you see double quotes (") or single quotes ('), be sure to use what I show and make sure they’re straight and not curly.
To understand programming, you need a bit of patience and the ability to think logically about a topic. You don’t need to be a computer guru or a hacker. You don’t need to be able to build a computer or take one apart (although that might be fun!). You don’t need to know a bit from a byte or how many programmers it takes to screw in a new light bulb.
However, I do need to make some assumptions about you. I assume that you can turn your computer on, that you know how to use a mouse and a keyboard, and that you have a working Internet connection and web browser. You should also know how to find and launch programs on your computer.
In this book, I explain everything else you need to get set up and coding in Ruby.
Here’s a list of the icons I use in this book to flag text and information that’s especially noteworthy.
The Technical Stuff icon highlights technical details that you may or may not find interesting. Feel free to skip this information, but if you’re the techie type, you might enjoy reading it.
The Tip icon highlights helpful tips that show you easy ways or shortcuts that will save you time or effort.
Whenever you see the Remember icon, pay close attention. You won’t want to forget the information you’re about to read — or, in some cases, I remind you about something that you’ve already learned that you may have forgotten.
Be careful. The Warning icon warns you of pitfalls to avoid.
I’ve put together a lot of extra content that you won’t find in this book. Go online to find the following:
Cheat Sheet:
An online Cheat Sheet is available at
www.dummies.com/cheatsheet/rubyforkids
. Here, you find information on basic Ruby statements, conditions, loops and objects; a list of words that can’t be used as Ruby variables or methods; a list of some of the useful methods provided by common Ruby classes; descriptions of common errors and what may cause them; and some small snippets of useful Ruby.
Web Extras:
Online articles covering additional topics are available at
www.dummies.com/extras/rubyforkids
. In these articles, I cover things like good ways to organize your Ruby class, some common Ruby shortcuts (also called “idiomatic Ruby”), Ruby troubleshooting tips, and more.
Programming is a blast, and doubly awesome with Ruby. Even Ruby’s creator wants you to have fun! After you learn the basics, you’ll start to find all kinds of things you can do with your newfound powers.
I’m very interested to hear how it goes as you learn Ruby! If you want to show me your new ideas, bug fixes, or enhancements to my projects, or if you have programs you come up with on your own, you can do so on Facebook (www.facebook.com/mobirobo), on Twitter (www.twitter.com/mobirobo_inc), or via email at [email protected].
Part I
In this part …
Getting Started with Ruby
Big Numbers
Bigger Hello World
For Dummies can help you get started with lots of subjects. Visit www.dummies.com to learn more and do more with For Dummies!
Project 1
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.
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:
Pull the chair out.
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:
Pull the chair away from the desk.
Walk around so your body is in front of the chair.
Turn around so your backside is facing the chair.
Make sure your body is exactly next to the chair.
Start bending your knees and lowering your body.
Keep bending your knees until your bottom makes contact with the seat of the chair.
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?
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!
Most obviously, you need a computer that’s running a current version of a consumer desktop operating system (Mac OS X or Windows).
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.)
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.
Go to
http://rubyinstaller.org
in your web browser.
Click the big red Download button.
A list of RubyInstallers appears.
Click Ruby 2.2.2 near the top of the RubyInstallers list (see Figure 1-1).
Do not click Ruby 2.2.2 (x64).
An installer program downloads to your computer.
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).
Figure 1-1: Click Ruby 2.2.2 to download installer.
Figure 1-2: Setup Ruby installation settings.
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:
Go to the
http://rubyinstaller.org
in your web browser.
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.
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.
Enter
C:\DevKit
for the location, as shown in
Figure 1-5
.
Figure 1-4: Download the Development Kit for Ruby 2.0 and above. Be sure to click the 32bits version.
Figure 1-5: Select the DevKit installation directory.
Now you need some final setup. Follow these steps:
Open your Windows launch screen (or use the Start Menu).
Click the Start Command Prompt with Ruby program (my machine looks like Figure 1-6).
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!
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:\Users\chris>
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:\Users\chris > cd \DevKit
C:\DevKit>
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.
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!
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
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.
Figure 1-6: Click Start Command Prompt with Ruby.
Figure 1-7: Initialize the Development Kit.
Figure 1-8: Install the Development Kit.
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:
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.
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!
Figure 1-10: Download the Atom Editor.
Figure 1-11: The Atom welcome screen.
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.
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.
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:
Open the
Applications
folder, and then open the
Utilities
folder (see
Figure 1-13
).
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$
In this book, I omit the full prompt and just show the $ to save space.
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.
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).
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.
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.
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.
If you’re just learning about command-line programs on Mac OS X, you may be unfamiliar with using sudo. sudo