Footnotes

Preface

1. Specifically, the examples all use Ruby-1.9.1-p430.

2. Especially me!

Acknowledgments

1. Well, originally they were Chapters 11, 12, and 13, and then they became Chapter 10 before settling down as 15 and 16. Now you know why those days were so dark.

Chapter 1

1. I had developed this talent over numerous lunches, happy hours, and late-night bull sessions. Oh, how I do devote myself to the cause.

2. But do take a good long look at the Document class because it’s going to be with us for much of the book.

3. The “problem like this” in the example is that it was written with randomly mixed tabs and spaces, with each tab worth two spaces. Now expand the tabs to four spaces each and you have the formatting chaos that we see.

4. Ruby also supports multiline comments delimited by =begin and =end, but Ruby programmers tend to stick with the # style of comments most of the time.

5. Because of its low, streamlined look, this naming style is known as “snake case.”

6. In fact, given the formatting limitations of this book, a good number of the blocks you’ll see in this book will be multiline rather than single line, simply because the longer line will not fit on the page.

7. Perhaps you should get out more often.

8. If you do look you will discover that I reformatted the comments a bit to make them fit on the page.

9. Note that the Float('3.14159') is not quite the same as '3.14159'.to_f. The Float method will throw an exception if you pass it bad input, while to_f will quietly return 0.

Chapter 2

1. Almost. The code block in the each version actually introduces a new scope. Any variables introduced into a code block are local to that block and go away at the end of the block. The more traditional for version of the loop does not introduce a new scope, so that variables introduced inside a for are also visible outside the loop.

2. That’s three equals signs!

Chapter 3

1. Well, you get the same hash if you are using Ruby 1.9, which is where this shorter hash literal syntax made its appearance.

2. In Ruby 1.9 it can be anywhere. In 1.8 the starred argument needed to be at the end of the argument list.

3. The star actually works the other way too: If you happen to find yourself holding a three-element array and want to pass that trio of objects to a method that takes three arguments, you can simply say some_method( *my_array ). Ah, symmetry.

4. That is, the end of the argument list not counting any explicit block argument. More about this in Chapter 19.

5. Well, superclasses and mixed-in modules.

6. . . . and fix: A much cleaner way to get rid of those pesky negative numbers—a way that actually works—is to simply say array.delete_if {|x| x < 0}.

Chapter 4

1. Do be aware that Ruby will print backslashes embedded in strings in different ways, depending on how you output the string. If you use puts to print out a string with a backslash, Ruby will just print the backslash. If you use pp, however, Ruby will print the same double backslash you would use to embed the backslash character in the string. Either way, it’s the same string with the same (single) backslash.

2. Strictly speaking, chomp removes at most one record separator character from your string. Since the record separator is by default your system’s newline character, most of the time this is a distinction without a difference.

3. The difference between bytes and characters isn’t just the difference between a single number and an associated character. If you happen to be dealing with the multi-byte characters—common in many Asian languages—then your strings can have many more bytes than characters.

4. This was not always the case. In pre-1.9 versions of Ruby, strings did have an each method that iterated over—wait for it—lines! This somewhat less than intuitive method disappeared in 1.9.

5. The code is slightly edited to fit on the page.

6. Again, the code is edited to fit on the page.

Chapter 5

1. See the Appendix for the names of a couple of books on regular expressions.

2. As we will see in a bit, you can turn off regular expression case sensitivity.

3. Well, any single character except a newline character, but keep reading.

4. As long as the letter, if there is one, is lowercase.

5. Regexp is the name of the Ruby regular expression class.

6. It can also go the other way, from the Time object to a string.

7. As does mine, so I know the bonehead of which I speak.

Chapter 6

1. If you are not familiar with ActiveRecord, don’t worry. In ActiveRecord there is a class for each database table. In our example we have the (unseen) Book class that knows about the books table. Every ActiveRecord table class has a class method called find, which takes various arguments telling the method for what it should search.

2. For more on object equality, see Chapter 12.

3. Or at least until your Ruby interpreter exits.

Chapter 7

1. You Python programmers know who I’m talking to!

2. Ruby does add some cool twists to its fairly vanilla object model, twists that we are going to explore in Chapters 13, 16, and 24.

3. The reason I hedge is that Ruby 1.9 added the BasicObject class, as the new superclass of Object. BasicObject instances are, however, few and far between and tend to be used in a way that masks their true identity. All these mysteries will be explained in Chapter 22.

4. This is a bit of a simplification, but not much. Many of the Object methods actually live in the Kernel module, which is mixed into the Object class, a process that we will explore in Chapter 16. For most practical purposes this is a distinction without a difference.

5. The print method prints what you tell it to, without mixing in any additional newline characters the way that puts does. The gets method is the inverse of puts: It reads a string.

6. The require method can also handle loading native libraries.

7. Except for those BasicObject oddballs.

8. The only instance!

Chapter 8

1. Again, to keep things simple we are going to start over here with the very minimal functionality of the original Document class of Chapter 1.

2. As in, “If it walks like a duck and quacks like a duck, then it must be a duck.”

3. Actually, most Ruby programmers would call File.open with a block, but that is beside the point here.

4. I did take some liberties with this code to make it fit within the formatting restrictions of this book.

Chapter 9

1. I’d like to invite you to stop for a minute and think about how this could possibly work. How do those tests get run when you just feed your test class declaration into the Ruby interpreter with no main program? I’m afraid you will have to wait until Chapter 20 for the answer.

2. You can find out all about RSpec—including all the ways that you can say what should happen—at www.rspec.info.

3. Note that spec is an operating system command, along the lines of ls or dir, not something you would say in Ruby.

4. http://github.com/thoughtbot/shoulda

5. http://mocha.rubyforge.org

6. http://rubyspec.org

7. Or at least it acts like a class.

Chapter 10

1. Well, it does lose all the white space in the text and won’t react well to punctuation. Close enough for an example.

2. That’s or as in ||.

Chapter 11

1. This will not work in versions earlier than Ruby 1.9, which greatly expanded the number of operators you can define.

2. You could argue that this method is a bit too simple. After all, we just throw away the author and title of the second document. This is fine for an example, but it is perhaps an issue in real life.

3. Don’t get the idea that all is lost with making String cooperate with our Document class. As we will see in Chapter 24, we can teach an old String some new tricks.

4. As all the former C programmers sit up and take notice.

Chapter 12

1. It’s the study of animals like the Loch Ness Monster and Yeti, animals that may or may not exist. Probably not.

2. Wait for it!

3. Remember, on average you are only going to have to search down half the list before you find what you are looking for.

4. Starting in version 1.9, Ruby hashes have a list superimposed atop the whole bucket structure, a list that keeps track of the order of the hash keys, because the natural workings of a hash will randomize the keys very thoroughly.

5. According to the American screenwriter Aaron Sorkin, good writers borrow from other writers. Great writers steal from them outright. Apparently Sorkin stole this line from Oscar Wilde.

Chapter 13

1. Let me also hasten to add that the term “singleton” as it is used here has nothing to do with the Singleton Pattern of design patterns fame. It’s just an unfortunate collision of terminology. If you feel a bit put out by this, consider the plight of the guy who wrote the Ruby design patterns book.

2. Well, any object except for instances of the numeric classes and symbols, neither of which supports singleton methods.

3. Singleton classes are also known as metaclasses or eigenclasses. Although terminology is mostly in the ear of the beholder, I like the more descriptive and less pretentious “‘singleton.”

4. In fact, since the average Ruby object never uses its singleton class, Ruby implementations will typically delay creating the singleton classes until they are actually needed. This is, however, just an implementation issue. If you do look, the singleton class will always be there for you.

Chapter 14

1. Also imagine that, as usual, we have reset the Document class back to its primordial, Chapter 1 form.

2. Yes, that’s the word.

Chapter 15

1. This book, of course.

2. In fact, since class names are constants, it’s a very close match.

3. But see the next chapter for the full implications of including a module.

4. Well, about 72. The actual size of the point has varied with time and geography, but the modern value has settled on 1/72 of an inch or about 0.353 mm.

5. We’ll be talking about mixing modules into classes in the next chapter.

Chapter 16

1. Again, we are starting over with our original Document class.

2. The astute reader will notice that I have passed silently over the possibility of simply copying the code into both classes. We shall speak no more of such an unpleasant alternative.

3. DataMapper, which is maintained by a cast of, well, tens, can be found at www.datamapper.org.

4. In real life, the sqlite3 ErrorCode module is buried inside several other modules, which I have omitted here for the sake of brevity.

Chapter 17

1. If you are using a pre-1.9 version of Ruby, the each_character method is misnamed since before 1.9 Ruby strings were just collections of bytes (really just integers), not actual characters. So if you are using 1.8.X, the Document each_character method will yield a series of numbers, each number the ordinal number of a character in the string. With Ruby 1.9, strings really are strings of characters, and the each_character method will yield a series of one-character strings.

2. Mysteriously, arrays also implement all of these methods. This might lead you to suspect that the Array class includes the Enumerable module. You would be right.

3. Yes, without the “e”.

4. Do be aware that for performance reasons, JRuby disables ObjectSpace by default. This is one of the few incompatibilities between JRuby and the other Ruby implementations. You can un-disable it, but it is an incompatibility nevertheless.

Chapter 18

1. OK, I made up the shimmering flash part.

2. Of course, unexpected exceptions never appear in any of my code, but I understand other people do occasionally experience them.

3. The technical term for objects with this scope dragging property is closure, and some Ruby programmers will use the terms closure and block interchangeably.

4. Technically, silence turns the logging level up (or is it down?) to ERROR.

Chapter 19

1. The phrase “end of your parameter list” really does mean the end. The &block parameter goes after all the other stuff in your parameter list, including those starred catch-all parameters.

2. Just to make things even more exciting, Ruby supports a third way to create Proc instances, the proc method. The interesting bit is that in 1.8, proc was synonymous with lambda. In 1.9 it is more like Proc.new. Some days I wonder why I get of bed.

3. Edited, as they say on television, to fit your screen.

Chapter 20

1. YAML is a structured file format similar to, but more human friendly than, XML. If you have ever created a Rails database.yml file, you know what YAML looks like.

2. $! is a global variable that Ruby sets to the last exception raised.

Chapter 21

1. Actually, given that you have tests, you know it works.

2. More specifically, the default method_missing actually lives in the Kernel module, which is included by Object.

3. We will deal with the question of how you add a new method to the existing NilClass in Chapter 24.

4. As an alternative, built into Ruby is the autoload method, which allows you to specify which file any given class is to be found. While the autoload method can be useful, it is nowhere near as flexible as the const_missing-based technique.

Chapter 22

1. “Bad” in this context means abysmally horrible.

2. With apologies to Mission Impossible—the old TV series, you understand, not those dreadful movies.

3. Sadly, it seems that in software engineering there is always at least a little catch. If you look carefully at the BasicObject version of SuperSecretDocument, you will see that we needed to explicitly specify the scope of the Time class with ::. We need to do this because of the disconnected role of BasicObject as the sort of noble gas of Ruby classes.

Chapter 23

1. A real skeptic might observe that the whole FormLetter project is simply furthering the cause of junk mail and is therefore the work of the devil, but that’s another issue.

Chapter 24

1. In the world of integer arithmetic, division by zero will result in an exception. Things are not quite so clear cut in the fuzzier realm of floating-point numbers, where dividing by zero will either result in NaN or Infinity, depending on exactly what you divide.

2. Programming lore has it that the original name was guerrilla patching, which then morphed into gorilla patching (perhaps programmers simply can’t spell?), which somehow evolved into monkey patching.

3. Well, it has been nagging me.

4. Come to think of it, that’s one I need a lot!

5. When you are tired of contemplating the metaphysics of monkey patching, you might consider whether this is an example of module nesting gone wild.

Chapter 25

1. Well, technically the interpreter reads in the whole class first and then executes the class body.

2. Passing false to instance_methods says that we don’t want to see any inherited methods, only the methods defined directly by LessEmpty.

3. Not quite as empty a shell as we might like. It doesn’t clear out any class methods, class variables, and class instance variables. One of the best things about writing books is that I get to leave something like that as an exercise for the reader.

Chapter 26

1. Note the clever use of the initialize block!

2. The real attr_reader method, along with its siblings, is defined in the Ruby C code (or Java if you are using JRuby). If you think that this makes the implementations of those methods inaccessible, then you need to read Chapter 30.

3. All of them alike.

Chapter 27

1. Try as we might.

2. If you look carefully at the XmlRipper class you will see that there is also a before method, which operates as the mirror image of after. Although I’m generally against adding features that aren’t used simply because they “make sense,” this one makes so much sense that I’ll make an exception. For more on this kind of thing, see Chapter 31.

3. If this seems confusing, consider that instance_eval( &block ) is equivalent to self.instance_eval( &block ) and self is the newly created XmlRipper instance.

Chapter 28

1. My standard answer: “No.” Long pause. “He’s my brother.”

2. I’m over it now. Really.

3. Keep reading though, because we will fix the “no space” limitation in a bit.

4. In the interest of keeping the example simple, the comment addition ignores the very real possibility that the statement itself might contain a # character.

5. And no, the addition of the quotes does not make this valid Ruby susceptible to an internal DSL solution. Think about it. Since there is no comma between the two strings, Ruby would concatenate the two strings together, leaving us with the impossible job of trying to figure out where the XPath ends and the argument begins.

6. Treetop is by no means the only Ruby-based tool for building sophisticated parsers. There is, for example, RACC, which is the Ruby rendition of the venerable Unix program YACC. Like Treetop, RACC reads in a file that describes the syntax of your language and produces Ruby code that can parse the language.

7. You can learn all about Treetop at www.treetop.rubyforge.org.

8. Like mileage, your time and date will vary.

9. A parser that just happens to be built with Treetop.

10. We can deduce that at sometime in the past there must have been a non-Treetop parser for Treetop grammar files to kick things off.

Chapter 29

1. Note that using gems in pre-1.9 versions of Ruby was a bit more complicated. Before 1.9, the Ruby Gems infrastructure was not completely integrated into Ruby. Because of this, you needed to be sure that you ran require 'rubygems' somewhere in your program or added the equivalent -rubygems argument to your Ruby command line.

2. The -a parameter asks the gem command to print out all of the versions of the gem, instead of just the latest. The --remote parameter means that the command should list the gems stored in the default remote repository, not just on your local machine.

3. That’s Version Conflict Induced Insanity. VCIS is also known as DLL Hell or JAR file Hell, depending on your programming background.

4. TAR files are the Unix answer to ZIP files, a simple archive file format that allows a single file to hold a whole tree full of files and directories.

5. Actually, the two inner TAR files are themselves compressed—and thus have an additional .gz suffix.

6. And why not? We’ve worked on it long enough!

7. By default, this is the directory that gets included in the Ruby load path when your gem is installed.

8. If the gemspec file seems like a simple internal DSL, that’s because it is.

9. Historically, most gems where hosted by RubyForge, the Ruby community's one-stop super store for all your open source Ruby project needs. RubyForge provides a source repository, a bug-tracking system and a host of other nifty features for the Ruby community. As of early 2010, the main gem repository duties have been taken up by the Gemcutter folks.

10. Yet.

11. Hoe was developed by Ryan Davis; the web site can be found at http://seattlerb.rubyforge.org/hoe.

12. Of course, you will have to install hoe with the usual gem install hoe first.

13. Or a WordProcessor class and a WordProcessor module, which comes down to the same thing.

Chapter 30

1. To keep things simple, Figure 30-1 is very schematic. For various practical reasons, an actual MRI AST tree is a bit more complex than I’m showing here—a bit, but not much.

2. The funny .y name comes from the fact that parse.y is actually the half-code/half-grammar input to a parser-generating tool called YACC.

3. To be fair, each of those files is pretty lengthy and full of some fairly sophisticated C.

4. Also to be found at www.ruby-lang.org.

5. You can find it at http://jruby.org. The code here is from Jruby 1.4 RC1.

6. Ant is the traditional Java build tool, along the lines of rake.

7. I did edit the code a bit to make it fit on the page.

8. Yes, that suffix is .EXT, as in “extension,” not .TXT.

Chapter 31

1. Orwell, G. A Collection of Essays. San Diego, CA: Mariner Books, 1970.

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

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