Head First
Ruby
Jay McGavren
OREILLY
Beijing Cambridge Kln Sebastopol Tokyo
Head First Ruby
by Jay McGavren
Copyright 2015 Jay McGavren. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (.
Editors: | Meghan Blanchette, Courtney Nash |
Cover Designer: | Randy Comer |
Production Editor: |
Indexer: |
Proofreader: |
Page Viewer: |
Printing History:
April 2015: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the OReilly logo are registered trademarks of OReilly Media, Inc. The Head First series designations, Head First Ruby, and related trade dress are trademarks of OReilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and OReilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and the authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
ISBN: 978-1-449-37265-1
[LSI]
1 more with less
Code the Way You Want
Youre wondering what this crazy Ruby language is all about, and if its right for you. Let us ask you this: Do you like being productive? Do you feel like all those extra compilers and libraries and class files and keystrokes in your other language bring you closer to a finished product, admiring co-workers, and happy customers? Would you like a language that takes care of the details for you? If you sometimes wish you could stop maintaining boilerplate code and get to work on your problem, then Ruby is for you. Ruby lets you get more done with less code.
The Ruby Philosophy
Back in the 1990s in Japan, a programmer named Yukihiro Matsumoto (Matz for short) was dreaming about his ideal programming language. He wanted something that:
Was easy to learn and use
Was flexible enough to handle any programming task
Let the programmer concentrate on the problem they were trying to solve
Gave the programmer less stress
Was object-oriented
He looked at the languages that were available, but felt that none of them was exactly what he wanted. So, he set out to make his own. He called it Ruby.
After tinkering around with Ruby for his own work for a while, Matz released it to the public in 1995. Since then, the Ruby community has done some amazing things:
Built out a vast collection of Ruby libraries that can help you do anything from reading CSV files to controlling objects over a network
Written alternate interpreters that can run your Ruby code faster or integrate it with other languages
Created Ruby on Rails, a hugely popular framework for web applications
This explosion of creativity and productivity was enabled by the Ruby language itself. Flexibility and ease of use are core principles of the language, meaning you can use Ruby to accomplish any programming task, in fewer lines of code than other languages.
Once youve got the basics down, youll agree: Ruby is a joy to use!
Flexibility and ease of use are core principles of Ruby.
Get Ruby
First things first: you can write Ruby code all day, but it wont do you much good if you cant run it. Lets make sure you have a working Ruby interpreter installed. We want version 2.0 or later. Open up a command-line prompt and type:
ruby -v
When you type ruby -v at a prompt, if you see a response like this, youre in business:
Do this!
If you dont have Ruby 2.0 or later, visit www.ruby-lang.org and download a copy for your favorite OS.
Use Ruby
To run a Ruby script, you simply save your Ruby code in a file, and run that file with the Ruby interpreter. Ruby source files that you can execute are referred to as scripts, but theyre really just plain text files.
You may be used to other languages (like C++, C#, or Java) where you have to manually compile your code to a binary format that a CPU or virtual machine can understand. In these languages, your code cant be executed before you compile it.
Other languages:
With Ruby, you skip that step. Ruby instantly and automatically compiles the source code in your script. This means less time between writing your code and trying it out!
The Ruby way:
Use Ruby - interactively
Theres another big benefit to using a language like Ruby. Not only do you not have to run a compiler each time you want to try out your code, you dont even have to put it in a script first.
Ruby comes with a separate program, called irb (for Interactive Ruby). The irb shell lets you type any Ruby expression, which it will then immediately evaluate and show you the results. Its a great way to learn the language, because you get immediate feedback. But even Ruby professionals use irb to try out new ideas.
Throughout the book, well be writing lots of scripts to be run via the Ruby interpreter. But anytime youre testing out a new concept, its a great idea to launch irb and experiment a bit.
So what are we waiting for? Lets get into irb now and play around with some Ruby expressions.
Using the irb shell
Open a terminal window, and type irb . This will launch the interactive Ruby interpreter. (Youll know its running because the prompt will change, although it may not match exactly what you see here.)
From there, you can type any expression you want, followed by the Return key. Ruby will instantly evaluate it and show you the result.