Part 3. Ruby dynamics

Ruby is dynamic, like human nature.

Matz, at RubyConf 2001

The phrase Ruby dynamics is almost redundant: everything about Ruby is dynamic. Variables don’t care what class of object you bind them to, which means you don’t have to (indeed, you can’t) declare their type in advance. Objects get capabilities from the classes that create them but can also branch away from their classes by having individual methods added to them. Classes and modules can be reopened and modified after their initial definitions. Though Ruby is a language of objects, it can also be built with a functional mindset to produce beautiful and expressive code. Ruby is dynamic enough even to support both functional and object-oriented programming paradigms!

And those examples are just the beginning. In this last part of the book, we’ll look more deeply and widely than we yet have at the ways in which Ruby allows you to alter the execution circumstances of your program in your program

First, in chapter 13, we’ll look at object individuation, going into the details of how Ruby makes it possible for individual objects to live their own lives and develop their own characteristics and behaviors outside of the class-based characteristics they’re “born” with. We’ll thus circle back to one of the book’s earliest topics: adding methods to individual objects. But here, equipped with the knowledge from the intervening material, we’ll zero in much more closely on the underlying mechanisms of object individuation.

Chapter 14 looks at callable objects: objects you can execute. You’ve seen methods already, of course—but you haven’t seen method objects, which we’ll discuss here, as well as anonymous functions in the form of Proc objects. Strings aren’t callable themselves, but you can evaluate a string at runtime as a piece of Ruby code, and chapter 14 includes that (sometimes questionable) technique. The chapter will also introduce you to Ruby threads, which allow you to run segments of code in parallel.

Chapter 15 looks at the facilities Ruby provides for runtime reflection: examining and manipulating the state of your program and your objects while the program is running and the objects exist. Ruby lets you ask your objects for information about themselves, such as what methods they can execute at runtime; and a number of hooks are available, in the form of methods you can write using special reserved names, to intercept runtime events like class inheritance and module inclusion. Here we’re entering the territory of dynamic reflection and decision making that gives Ruby its characteristic and striking qualities of flexibility and power.

Chapter 16 examines Ruby’s potential as a functional programming language. Here you’ll learn how harnessing Proc objects and replacing state and variable assignment with functions can help to create powerful, expressive code. Functional programming is reviewed as a style of programming, which means it can be added to your toolbelt to use as circumstances or preferences warrant. To round out our study, we’ll review currying, partial function application, and tail-call optimization and how these techniques translate to Ruby.

This entire part of the book includes many best-practices pointers (and pointers away from some not-so-best practices). That’s not surprising, given the kind of ground these chapters cover. This is where your programs can distinguish themselves, for better or worse, as to the nature and quality of their use of Ruby’s liberal dynamic-programming toolset. It pays to think through not only the how and why but also the whether of some of these powerful techniques according to context. Used judiciously and advisedly, Ruby’s dynamic capabilities can take you to new and fascinating heights.

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

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