Appendix A. An Incredibly Brief Introduction to Ruby

Fortunately, you don’t need to know a whole lot of Ruby to get real work done with Rails. The creators of Rails have used many of Ruby’s most advanced features to make Rails easy to work with, but fortunately you can enjoy the benefits without having to know the details. This appendix explains the basics you’ll need to perform typical tasks in Rails and should help you get started. For a lot more detail on Ruby, try Learning Ruby (O’Reilly, 2007), The Ruby Programming Language (O’Reilly, 2008), the Ruby Pocket Reference (O’Reilly, 2007), or Programming Ruby (Pragmatic Programmers, 2004).

Note

If you’ve never worked with a programming language before, this appendix may go too fast for you. It’s hard to be incredibly brief and cover the basic basics at the same time. However, if you’ve worked with JavaScript before, you should be able to get started here.

Ruby is a beautiful but sometimes mystifying language, and probably a better choice as a second language to learn rather than a first language.

Because this is a Rails book, examples will work inside of the Rails framework, in a Rails view and controller, rather than from the command line. If you haven’t touched Rails before, it makes sense to read Chapter 1 first and get Rails installed, and then come back here for more instruction.

How Ruby Works

Ruby is an object-oriented language. Although it’s often compared to Perl, because Ruby code often looks like Perl, Ruby’s object-orientation goes much deeper. Practically everything in Ruby is an object.

What does that mean?

Objects are combinations of logic and data that represent a (usually mostly) coherent set of tasks and tools for getting them accomplished. Programming objects aren’t quite as concrete as objects in the real world, often created and destroyed (or at least abandoned for cleanup later) in fractions of a second. Nonetheless, in those brief moments—or in the hours, days, or years they could also exist—they provide a practical means of getting things done.

In some sense, a program is a big toolchest filled with these objects, and programming is about assembling objects to put into the chest. Ruby provides some starter objects and a means of creating new objects and, of course, ways to start these objects interacting with each other so that the program actually runs.

There are a few other important things to know about Ruby. They’re probably most important if you’re coming to Ruby from other programming languages that have different expectations, but they all affect the way you’ll write Ruby programs:

  • Ruby is an interpreted language, meaning that Ruby reads through the code and decides how to execute it while it’s running, rather than reading it and turning it into a highly optimized set of instructions before it actually runs. (There are a few people working on ways to create a compiled Ruby, but that’s unusual.) While that slows things down, it also adds a lot of flexibility.

  • Ruby also has really flexible syntax expectations. Most of the time this makes things easier—you don’t need to type parentheses around method parameters most of the time. Other times, however, you’ll find yourself facing mysterious error messages because you didn’t include parentheses and the situation is slightly ambiguous. (This book tries to warn you about these kinds of situations when they appear in Rails.)

  • Ruby uses dynamic typing.[2] Some languages (notably Java, C, and C++) expect that the programmer will always know, and always specify, the kind of information they expect to store in a given information container, a variable. Locking that down in advance makes it easy to do some kinds of optimization. Ruby has taken another path, leaving variables open enough to contain any kind of information and be created at any time. Again, this allows for a much more flexible approach, in which operations can change what they do based on context. Sometimes, however, it means that things can go wrong in strange and unpredictable ways if something unexpected is in a variable.

  • Ruby supports blocks and closures. You don’t need to know how to create methods that work with blocks or closures in order to use Rails, but you definitely do need to know how to call methods that expect a block of code as an argument. At first, your best choice for dealing with these features will be to look at sample code and use it as a foundation rather than trying to step back and figure out how this should work in the abstract.

  • Ruby lets advanced developers perform metaprogramming, even creating Domain Specific Languages (DSLs), which are kind of like their own miniature programming language focused on a particular task. You don’t need to know how to do metaprogramming to use Rails, but you should be aware that Rails uses these techniques. Sometimes you’ll encounter something that claims to be Ruby but seems very strange and too specialized to be part of the Ruby language. Odds are good that metaprogramming is involved. As with blocks and closures, it’s often best to start out by emulating sample code to work toward figuring it out.

Ruby is a very powerful language. It’s not hard to get started in Ruby, but you should at least be aware of these powerful techniques so you can recognize them when you encounter them. Knowing that these possibilities exist may help reduce your frustration when you encounter mysterious difficulties.



[2] Sometimes this is called “duck typing” because when Ruby processes information, “if it looks like a duck and quacks like a duck, it’s a duck.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset