Where to Go Next - Ruby Wizardry: An Introduction to Programming for Kids (2014)

Ruby Wizardry: An Introduction to Programming for Kids (2014)

Chapter 14. Where to Go Next

The Big Picture: What You Know

Man, what a story. What intrigue! What suspense! It was so astounding, I barely talked at all toward the end there. You probably noticed that. I pop in and out a lot.

It may seem like we haven’t come all that far, but if you remember back to when you first picked up this book, you didn’t know anything about Ruby at all. You’d probably never heard of Senior Apprentices to Royal Plumbers, Hasheries, or Dagrons, let alone strings, object IDs, or methods. Now you know about all those things and more!

We’ve covered so much, I feel it’s only right to do one last quick review. For my own sake. Just to keep everything straight in my scattered mind. Don’t worry—it’ll be real quick; if you need a more in-depth refresher, thumb back to the earlier chapters and reread the You Know This! sections (because you totally do, even if you don’t always remember every last detail).

image with no caption

We started out by learning how to install and set up Ruby. No small feat! We got Ruby up and running on our computer, learned how to run snippets of Ruby code with IRB, and discovered how to write files, called scripts, that let us collect a bunch of lines of code and run them all at once.

Once we figured out how to run code, the next step involved writing something that was interesting to run. Remember back when you only knew how to print strings and add numbers? When the thrill of programming was all about putsing phrases and multiplying things together? Those were the days! But once you start learning to write stories that you can make happen in real life—that is, programs—you want to write bigger and better ones as quickly as you can. So we moved on to control flow (using if, elsif, else, and unless) and Booleans (true and false), and in no time flat, we were controlling the way information moved through our programs, repairing the Mysterious Pipe, and guiding Haldo through an underground maze.

Then we were really off to the races! We started talking about Ruby loops and iterators, using methods like each to print out all the values in an array (which is like a list of items: [1, 2, 3]), update a hash (which is like a short dictionary of items and values: { name: Lou, fancy: true }), and help Hank and Squeaky Jim fix the Hashery computer. We learned about ranges, which are just a series of numbers or letters (like (0..5) or ('a'..'z')), and symbols, which are simply names or labels we use in Ruby. For instance, we saw them used as hash keys, as in { hamburgers: 'delicious' }.

That’s when things started getting interesting! Scarlet found a python scale on the Hashery floor, leading the group on a voyage to the Carmine Pines. We met the Off-White Knight, who showed us how to create our very own methods using the def keyword. We also saw how to do all kinds of fancy method tricks, like setting default arguments, using splat parameters, and writing methods that can take blocks by using the yield keyword.

We went on to meet the Dagron, who explained Ruby objects and classes (which are just objects that create other objects). We learned about where in a program our variables could be seen and used, covering global, class, instance, and local variables. We even learned about a special Ruby value, self, that refers to the current object! As we dove deeper into Ruby class syntax, we covered attr_reader, attr_writer, and attr_accessor, which are shortcuts we can use so we don’t have to write methods to get and set instance variables every time we want to update them from outside our class.

We also discovered (courtesy of the Queen) that some classes can inherit from others—that is, they take on some of the properties and abilities of other classes—and that classes can let us reuse code through inheritance. We saw that classes that inherit from other classes can override methods from their superclass—for example, a GuardDog that inherits from Dog can have its own version of a bark method that does something different from what Dog’s bark method does—and that we can always “reach up” to the superclass and call one of its methods using super.

The Queen also taught us about modules, which are just like Ruby classes except we don’t create instances of them! They’re for mixing in behavior by using include or extend, which allow us to reuse code from lots of sources while still using only one superclass. Modules are also fornamespacing, or organizing our code so that we don’t have to cram all of it into one file.

By using modules, we can easily control where all our variables, constants, and methods are available in our Ruby programs, and we can create classes with one superclass that inherit behavior from a whole range of sources.

Then we voyaged on to the Refactory, where we learned how to rewrite our code to make it clearer without changing the way it behaves. We saw a bunch of cool tricks for improving our Ruby, including breaking up big methods into smaller ones and removing duplicate code so that each method did one thing and did it well. We also learned about file input/output at the Refactory, including how to open, read, and write files with Ruby.

Finally, we covered Ruby and the Internet, learning about Ruby gems, and web servers like WEBrick, and useful websites like the RubyGems site and GitHub. We saw the King, the Queen, Scarlet, and Ruben use their Ruby knowledge to catch the pythons and ultimately show them that Ruby and Python are both excellent programming languages, and you used your Ruby knowledge to arrive here, at the end of this book, chockfull of wisdom. I couldn’t be prouder! That’s the honest-to-gravy truth.

Additional Resources and Further Reading

While there’s a lot of Ruby magic in these pages, we didn’t cover everything there is to know about Ruby. There is a huge number of amazing books and websites that will help you learn even more about Ruby and programming, and I’ve listed a bunch here! Don’t feel pressured to read all (or any) of these books or sites—they’re just new places to keep learning Ruby now that you’ve finished this book.

Beginner Books

These books are great for beginners.

Beginning Ruby, 2nd Edition, by Peter Cooper (Apress, 2009). This is just what it sounds like! A fantastic introduction to Ruby.

Programming Ruby, 4th Edition, by Dave Thomas, Andy Hunt, and Chad Fowler (The Pragmatic Bookshelf, 2013). This is sometimes called the “pickaxe book,” because it has a huge picture of a pickaxe on the cover. Lots of Ruby programmers say this is their go-to Ruby book, and I can promise there’s not a single question about Ruby you might have that isn’t covered in here.

The Ruby Programming Language by David Flanagan and Yukihiro Matsumoto (O’Reilly Media, 2008). Yukihiro “Matz” Matsumoto is the creator of the Ruby language, so he’s got lots of great insights into what Ruby can do and what makes it great. This is another awesome introductory Ruby book.

Why’s (Poignant) Guide to Ruby by why the lucky stiff (http://en.wikipedia.org/wiki/Why’s_(poignant)_Guide_to_Ruby). This is the Ruby book I first learned from, and if you read it carefully, you’ll see I snuck a few of why’s tricks and jokes into this very book. why’s guide is a magical, manic, illustrated guide to Ruby. When you’re done with this book, pick up that one! I promise you’ll be glad you did.

Intermediate Books

These books are a bit more advanced.

Design Patterns in Ruby by Russ Olsen (Addison-Wesley Professional, 2007). This book is about good patterns for writing your Ruby code. You’ll learn from the pros!

Eloquent Ruby by Russ Olsen (Addison-Wesley Professional, 2011). Want to learn to write code like a native Rubyist? Read this book!

The Well-Grounded Rubyist by David Black (Manning Publications, 2009). Imagine a book just like this one, only with fewer magical creatures and more stuff about fancy Ruby topics like threads and error handling. All imagined? Congratulations! You’ve imagined The Well-Grounded Rubyist. (Imagine that!)

Wicked Cool Ruby Scripts by Steve Pugh (No Starch Press, 2008). If you’re looking for a fanciful cookbook, a veritable cornucopia of Ruby script recipes for everything from games to web servers, pluck this book off your nearest store shelf (or ask your folks to order it off the Internet).

Advanced Books

These books are the most complex of all!

Metaprogramming Ruby 2 by Paolo Perrotta (The Pragmatic Bookshelf, 2014). This book explores the deep, dark secrets of how Ruby can read its own code and change it while it runs! Not for the faint of heart.

Practical Object-Oriented Design in Ruby by Sandi Metz (Addison-Wesley Professional, 2012). If you want to write Ruby like a pro, you can’t do better than this book. Your objects will sparkle and your classes will gleam when you’re done!

Ruby Under a Microscope by Pat Shaughnessy (No Starch Press, 2013). This book goes straight to the core of what makes Ruby . . . well, Ruby! It covers all the details of how all those little bits and bytes, zeros and ones, turn into Ruby code. If you read this book and understand every bit of it, please call me up and explain it.

Online and Multimedia

Railscasts

(http://railscasts.com/)

Ruby on Rails is a popular framework, or set of tools, that Ruby programmers use to make web applications. These screencasts by Ryan Bates show you how Ruby code powers Rails applications, and Ryan’s instructions make it easy to follow along at home. As with Ruby Tapas, not all these screencasts are free, so you’ll need your local adult’s help to sign up if you want to be able to watch them all.

Ruby5 podcast

(http://ruby5.envylabs.com/)

This is more Ruby news, but in podcast form! If you like listening more than reading, this one’s for you.

Ruby Rogues

(http://rubyrogues.com/)

This is another podcast, though I think this one is more like listening to blog posts than it is like listening to the news. If you’re interested in hearing more about the ins and outs of Ruby, its gems, and its tools, give the Ruby Rogues a listen.

Ruby Tapas

(http://www.rubytapas.com/)

For those of you who like to watch videos instead of read or listen, these screencasts by Avdi Grimm give you short introductions to different parts of Ruby that you might not know about. Only a few of the screencasts are free, though, so if you want to watch them all, you’ll need your local adult’s help.

Ruby Weekly

(http://rubyweekly.com/)

Curated by the very same Peter Cooper who wrote Beginning Ruby, this once-a-week email is packed with helpful Ruby articles, tutorials, and videos. It’s pretty advanced stuff, but after you’ve been writing Ruby for a while, it’ll be the first place you look for Ruby news.

Interactive Resources

Codecademy

(http://www.codecademy.com/tracks/ruby/)

If you ever want to practice running Ruby in the comfort of your own web browser, you can do the Ruby lessons over at Codecademy. Fun fact: I wrote all these courses, so they should seem pretty familiar! The only downside is that they cover a lot of the same information you’ve learned here, so they might be a little too familiar. If you want to practice writing code and reviewing what you’ve learned, though, head on over and start typing—it’s free! (You must be 13 or older to create an account.)

Code School

(https://www.codeschool.com/paths/ruby/)

Code School is another great website for learning Ruby. Like Codecademy, it requires that you be 13 or older to create an account, and unlike Codecademy, it isn’t free. It’s got lots of good videos, though, so if you learn best by watching someone else work, this might be the site for you.

Ruby Koans

(http://rubykoans.com/)

Imagine if you took all the knowledge of this book and divided it up into a bajillion wise little sayings in the form of Ruby programs. Those are the Ruby koans! If you’re looking for more practice and love to learn by doing, go ahead and download the koans and start programming. They also do a great job of teaching you about testing, since each koan is like a failing test that you have to fix to get it to pass; the more lines of code you repair, the greater your Ruby enlightenment.

Ruby Monk

(https://rubymonk.com/)

This is sort of like a mix between Codecademy’s courses and the Ruby Koans. The more exercises you complete, the further along the path to Ruby mastery you’ll progress!

Additional Topics

We talked about tons and tons of Ruby goodness, but there are a few bits and trinkets of the language we didn’t get to—mostly because they’re not quite as much fun as what we covered, plus these topics are a bit on the trickier side. If you’re curious and want to learn more about them, though, I’ve dashed off a quick list here.

The Enumerable module

You might have wondered during our adventures how both arrays and hashes know how to use the each method. It’s because they both mix in the Enumerable module, which you can use directly in your own classes to simplify your code! It includes all kinds of handy methods like all?,any?, include?, and find. You can read all about it in the official docs: http://ruby-doc.org/core-2.0.0/Enumerable.html.

Regular expressions

Regular expressions are like a mini-language inside Ruby that lets you match patterns in words or phrases. For example, you could use them to find strings that contain only uppercase letters or check whether a string is a valid email address. Regular expressions exist in many languages, but you can find some Ruby-specific information at http://www.regular-expressions.info/ruby.html. You can also use a free tool called Rubular at http://rubular.com/. Rubular lets you test out your regular expressions in real time, so you can see what your patterns match and what they don’t.

Procs and lambdas

These are not quite blocks and they’re not quite methods—they’re somewhere in between! You can think of them either as methods without names or as “saved” blocks you can run over and over. You can learn more about them in the Ruby Monk exercises (see Interactive Resources).

Information hiding

You can keep information in your Ruby classes more secure by using the private and protected methods. These come in handy when you’re writing Ruby as part of a group or team, and while they don’t completely prevent other programmers from using methods they shouldn’t, they can help your teammates understand which methods they can rely on and which ones are still “under construction.” You can read more about the public and protected methods in the Ruby docs: http://www.ruby-doc.org/core-2.0.0/Module.html#method-i-private.

Handling exceptions

Every now and then, we saw our Ruby code throw an error. Usually I explained why it happened and we went on our merry way, but if you think about it, that’s not always the best way to do things. Sometimes when an error (also called an exception) happens, we want to do something about it, like set a default value or print a message to the screen. Handling exceptions in Ruby is called—you guessed it—exception handling, and if you want to learn more about it, you can read Avdi Grimm’s Exceptional Ruby (http://exceptionalruby.com/).

Reflection/metaprogramming

Remember when we talked about file I/O, we saw it was possible to write Ruby code that writes Ruby code? This means that Ruby has the ability to look at its own code and change it! The inward-looking part is called reflection, and the ability for Ruby to change its own programming is called metaprogramming. This is some of the hardest Ruby code to write, but if you’re feeling up to it, you can learn all about it from Paolo Perrotta’s book Metaprogramming Ruby.

Debugging

We talked a little bit about fixing errors in our code, but we didn’t talk about writing tests for it or debugging (that is, fixing) it in a systematic way. Writing tests to prove your code is correct and becoming good at debugging it are very important skills for any programmer to have. If you’re interested in learning more about both, you can read about the built-in Ruby testing library, MiniTest, in the Ruby documentation at http://ruby-doc.org/stdlib-1.9.3/libdoc/minitest/spec/rdoc/MiniTest/Spec.html. If you’re feeling particularly adventurous, you can read about my favorite testing library, RSpec, at http://rspec.info/.

Threads and processes

In all our Ruby programs, we really only did one thing at time: we’d set a variable and then use it, or maybe we’d iterate over an array and print each item to the screen. We never really did two things at exactly the same time. With Ruby threads and processes, it’s possible to do two things at once! As you might imagine, juggling multiple things at once is many times harder than handling just one process at a time, so learning to use Ruby threads and processes takes some practice. If you want to learn more, you can read Jesse Storimer’s Working with Ruby Threads(http://www.jstorimer.com/products/working-with-ruby-threads/). Careful—this one’s really advanced!

Creating websites

Finally, while we did talk about Ruby web servers like WEBrick, we didn’t talk much about creating entire websites with Ruby. You may have heard of Ruby on Rails (I mentioned it when describing Railscasts in Online and Multimedia), which is a big library of code made up of many gems that helps make writing websites with Ruby easier. It’s a good way to build websites and very popular, but sometimes newer Ruby programmers have trouble understanding all the things it does and decisions that went into making it. If you want to make websites with Ruby, you might want to start with a smaller, simpler program (and one of my favorites) called Sinatra. You can find it online at http://www.sinatrarb.com/.

I admit it: I’ve been dragging my feet. I don’t want the book to end! But alas, I’ve dispensed all my Ruby wisdom. Now you know everything I know, plus you’ve got all the smarts and experience of the King, the Queen, Ruben, Scarlet, and all of their friends combined. I knew you could do it! I believed in you from the start. So even if the book has to end, at least it ends with me being right!

When you close this book, I want you to do one thing: fire up your own personal Computing Contraption and write yourself a Ruby program. It can do anything you want, big or small, silly or serious. Don’t worry if it breaks! The only way we learn is by writing programs and breaking them and fixing them and making them better, so it’s perfectly okay if your program breaks or doesn’t do what you want at first. You’re writing stories and poems for a machine, and the biggest part of the adventure isn’t having a finished, perfectly working program—it’s all the crazy things that happen along the way.

So go! Go write the best program you can, and have fun. I’ll be seeing you.

image with no caption