To my loving wife. You're a true gem!
FOREWORD
One of the great joys of using Ruby is that the language is engineered to make for easy and effective procedural, script-style programming. Wicked Cool Ruby Scripts , as its title suggests, shines the spotlight on the scripting side of Ruby.
It's nice to see attention going in this direction. People often describe Ruby as a "scripting language," but that misses the point. Ruby is a general-purpose, object-oriented programming language that happens to offer a rich syntactic toolkit for scripting. I won't try to define the latter rigorously, but it has something to do with a procedural approach and with an acknowledgment of the fact that short programs that don't spin off library modules can be okay too. I believe that this kind of programming is one of Ruby's sweet spots, precisely because the value of allowing for it was not an afterthought in the design of the language.
Steve Pugh has been on a journey through a varied, many-faceted landscape of scripting tasks and solutions, and he has parlayed that journey into an engaging, illustrative, instructive travelogue. He's got us encrypting files, parsing RSS feeds, adding users to our systems, developing network exploits (but on the side of the good guys), scraping images, making phone calls, quicksorting arrays, solving Sudoku puzzles, and plenty more. Steve's delight in Ruby's versatility is contagious; when I look at Wicked Cool Ruby Scripts , I'm reminded of the power of the "Wow!" factor in Ruby and of Matz's dictum that Ruby is optimized for programmer pleasure and fun. Writing scripts is, moreover, an extraordinarily useful form of fun.
So enjoy this book. You'll learn a lot of Ruby, find lots of techniques to borrow and tweak, probably get a lesson in an algorithm or two that you're rusty onand have a chance to delve into one of the strongest areas for this wonderful language.
David A. Black
Director, Ruby Power and Light, LLC
North Brunswick, New Jersey
November 2008
ACKNOWLEDGMENTS
First and foremost, I would like to thank my family, who supported me during the authoring of this book. I definitely wouldn't have finished it if not for their patience and support. My wonderful wife, Shannon, who puts up with me being a nerd, gave valuable insight into the English language. It turns out that I'm better at writing code than English sentencesthe spellcheckers can only take me so far.
I would be remiss if I did not thank the man who introduced me to RubyLim Vu. Without his guidance and friendship, I doubt I would be involved in Ruby today. I still remember our conversations about Ruby when I'd pick his brain to find out more about this new language.
I also have to thank David Black for constantly answering my questions, giving solid advice, offering honest critiques, and challenging the syntactical slip-ups that are inherent to writing code in many different languages. Some of my scripts originally looked more like Perl than Ruby, so many thanks to David for keeping me accountable and giving wonderful guidance. The guy truly has the heart of a teacher.
Finally, I would like to thank the entire team of people at No Starch Press for publishing this book on Ruby. Bill Pollock has put together a remarkable group of people who make authoring a book a wonderful experience. Thanks to Tyler Ortman, who put up with my random phone calls, questions, and ideas, while still managing to keep the book on point and readable. His gentle words of encouragement and motivation were great. Finally, I owe Megan Dunchak many thanks for putting the copyediting phase on auto-pilot. If it weren't for all of the people above, I wouldn't be writing these acknowledgments, and you wouldn't be reading them.
INTRODUCTION
So you've read a few introductory books about Ruby, you have a good feel for the syntax, and you've written "Hello, world!" about 20 different optimized waysnow what? This book is intended to give your mind an exercise in Ruby as a scripting language.
Scriptingthat is, automating repetitive tasks you're not prepared to do yourselfis one of those things that a good language must be able to do. Perl and Python are two of the most widely known scripting languages, and their popularity has a direct correlation to their usefulness for scripting. Writing utilities should not be a cryptic task, nor should scripts take an inordinate amount of time to create. It is in that spirit that Ruby was born.
The designer of Ruby, Yukihiro "Matz" Matsumoto, had an overall vision to create a language that would be easy to use in everyday tasks. He felt the language should be fun to write so that more energy could be put toward creative processes with less stress. When they aren't distracted by the peculiarities of a language, programmers can focus on writing better programs. When you need to write a script, the last thing you want to worry about is weird syntax and compilers interpreting your code. Ruby is written in such a way that the syntax and programming flow is natural. I have found Ruby to be comfortable to work with because of its natural syntax, wonderful community, and simplistic view of how programming should be. As you will see throughout this book, Ruby makes scripting a breeze!
A command of scripting in Ruby is an invaluable skill to have. You will find that by automating repetitive tasks, you will free up much of your time to do more important things like writing other wicked cool Ruby scripts. I hope this book helps you see the many uses of Ruby and makes you a better programmer.
Wicked Cool Ruby Scripts
What differentiates a wicked cool Ruby script from an ordinary script? A wicked cool script is something that is useful, challenging, and, above all, educational. Not educational in the schoolmarm sense of the word, but rather as a stimulus to use Ruby to its full potentialto push the language to its limits.