Pat Shaughnessy is a Ruby developer working at McKinsey & Co., a management consulting firm. Pat was originally trained as a physicist at MIT, but later spent more than 20 years working as a software developer using C, Java, PHP, and Ruby, among other languages. Writing Ruby Under a Microscope has given him an excuse to reuse bits of his scientific training while studying Ruby. A fluent Spanish speaker, Pat frequently visits his wifes family in northern Spain. He lives outside of Boston with his wife and two children.
Foreword
Oh, hi! I didnt see you come in. I dont want to be too forward, but let me preface this by saying you should buy this book!
My name is Aaron Patterson, but my Internet friends call me tenderlove. I am on both the Ruby core team and the Ruby on Rails core team, and I did the technical review of this book. Does that mean you should listen to me? No. Well, maybe.
Actually, when Pat approached me to do the technical review of this book, I was so excited that my top hat fell off and I dropped my monocle in my coffee! I knew about Pats previous work on Ruby Under a Microscope , and the idea of making an updated and print version available made me really happy. I think many developers are intimidated by Rubys internals and are afraid to dive in. Quite often people ask me how they can learn about how Ruby works under the hood or where to get started hacking on Ruby internals. Unfortunately I didnt have a good answer for peopleuntil now.
Pats style of writing, in combination with experimentation, makes Ruby internals very approachable. The experiments are combined with explanations of Rubys internals such that you can easily understand why Ruby acts the way it does with regard to behavior and performance. Next time you encounter some behavior in your Ruby code, whether it be with performance, local variables and your environment, or even garbage collection, this book wont just tell you why your code behaves the way it does, but will even tell you how .
If youre someone who wants to start hacking on Rubys internals, or if you just want to understand why Ruby acts the way it does without any handwaving, this is the book for you. I enjoyed this book, and I hope you will too.
Aaron Patterson <3 <3 <3 <3
Acknowledgments
I could never have finished a project like this without the support of many different people!
First of all, thanks to Satty Bhens and everyone else at McKinsey for giving me the flexibility to write a book and keep my day job at a great company. Alex Rothenberg and Daniel Higginbotham gave me invaluable advice, suffered through reading many early drafts, and helped me throughout the process. Special thanks to Xavier Noria, who took an interest in the project early on, gave me fantastic feedback on the entire rough draft, and was also the inspiration behind . Santiago Pastorino reviewed the rough draft as well. Jill Caporrimo, Prajakta Thakur, Yvannova Montalvo, Divya Ganesh, and Yanwing Wong were my proofreading SWAT team. Self-publishing would have been much harder without your help. Finally, without the constant encouragement and support Peter Cooper has given me this year, I probably never would have attempted to write this book. Thank you, Peter.
Thanks to everyone at No Starch Press for helping me bring an expanded, updated version of Ruby Under a Microscope to print. The result is a book Im proud of and one the Ruby internals topic deserves. Thanks to Julianne Jigour, my copyeditor. My writing has never been so clear and easy to follow. Thank you, Riley Hoffman and Alison Law, for your editing advice and for beautifully reproducing hundreds of diagrams for print. Youve been a pleasure to work with. Thanks to Charles Nutter for the technical help and advice on JVM garbage collection. Special thanks to Aaron Patterson: This is a more interesting and accurate book because of your great suggestions and technical review. Finally, thanks to Bill Pollock for reading and editing every single line of text in the book. Your guidance and expertise have allowed me to write a book I could never have dreamed of writing on my own.
Introduction
What seems complex from a distance is often quite simple when you look closely enough.
At first glance, learning how to use Ruby can seem fairly simple. Developers around the world find Rubys syntax to be graceful and straightforward. You can express algorithms in a very natural way, and then its just a matter of typing ruby
at the command line and pressing enter , and your Ruby script is running.
However, Rubys syntax is deceptively simple; in fact, Ruby employs sophisticated ideas from complex languages like Lisp and Smalltalk. On top of this, Ruby is dynamic; using metaprogramming, Ruby programs can inspect and change themselves. Beneath this thin veneer of simplicity, Ruby is a very complex tool.
By looking very closely at Rubyby learning how Ruby itself works internallyyoull discover that a few important computer science concepts underpin Rubys many features. By studying these, youll gain a deeper understanding of what is happening under the hood as you use the language. In the process, youll learn how the team that built Ruby intends for you to use the language.
Ruby Under a Microscope will show you what happens inside Ruby when you run a simple program. Youll learn how Ruby understands and executes your code, and with the help of extensive diagrams, youll build a mental model of what Ruby does when you create an object or call a block.