Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide
Table of Contents
Foreword Preface Roadmap Ruby.new Classes, Objects, and Variables Containers, Blocks, and Iterators Standard Types More About Methods Expressions Exceptions, Catch, and Throw Modules Basic Input and Output Threads and Processes When Trouble Strikes Ruby and Its World Ruby and the Web Ruby Tk Ruby and Microsoft Windows Extending Ruby The Ruby Language Classes and Objects Locking Ruby in the Safe Reflection, ObjectSpace, and Distributed Ruby Built-in Classes and Methods Standard Library Object-Oriented Design Libraries Network and Web Libraries
http://www.rubycentral.com/book/index.html (1 of 2) [7/20/2001 9:48:32 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Microsoft Windows Support Embedded Documentation Interactive Ruby Shell Support
Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide" Copyright © 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0. This reference is available for download.
http://www.rubycentral.com/book/index.html (2 of 2) [7/20/2001 9:48:32 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Contents ^
Next >
Foreword Man is driven to create; I know I really love to create things. And while I'm not good at painting, drawing, or music, I can write software. Shortly after I was introduced to computers, I became interested in programming languages. I believed that an ideal programming language must be attainable, and I wanted to be the designer of it. Later, after gaining some experience, I realized that this kind of ideal, all-purpose language might be more difficult than I had thought. But I was still hoping to design a language that would work for most of the jobs I did everyday. That was my dream as a student. Years later I talked with colleagues about scripting languages, about their power and possibility. As an object-oriented fan for more than fifteen years, it seemed to me that OO programming was very suitable for scripting too. I did some research on the 'net for a while, but the candidates I found, Perl and Python, were not exactly what I was looking for. I wanted a language more powerful than Perl, and more object-oriented than Python. Then, I remembered my old dream, and decided to design my own language. At first I was just toying around with it at work. But gradually it grew to be a tool good enough to replace Perl. I named it Ruby---after the precious red stone---and released it to the public in 1995. Since then a lot of people have become interested in Ruby. Believe it or not, Ruby is actually more popular than Python in Japan right now. I hope that eventually it will be just as well received all over the world. I believe that the purpose of life is, at least in part, to be happy. Based on this belief, Ruby is designed to make programming not only easy, but also fun. It allows you to concentrate on the creative side of programming, with less stress. If you don't believe me, read this book and try Ruby. I'm sure you'll find out for yourself. I'm very thankful to the people who have joined the Ruby community; they have helped me a lot. I almost feel like Ruby is one of my children, but in fact, it is the result of the combined efforts of many people. Without their help, Ruby could never have become what it is. I am especially thankful to the authors of this book, Dave Thomas and Andy Hunt. Ruby has never been a well-documented language. Because I have always preferred writing programs over writing documents, the Ruby manuals tend to be less thorough than they should be. You had to read the source to know the exact behavior of the language. But now Dave and Andy have done the work for you. http://www.rubycentral.com/book/foreword.html (1 of 2) [7/20/2001 9:48:33 PM]
Programming Ruby: The Pragmatic Programmer's Guide
They became interested in a lesser-known language from the Far East. They researched it, read thousands of lines of source code, wrote uncountable test scripts and e-mails, clarified the ambiguous behavior of the language, found bugs (and even fixed some of them), and finally compiled this great book. Ruby is certainly well documented now! Their work on this book has not been trivial. While they were writing it, I was modifying the language itself. But we worked together on the updates, and this book is as accurate as possible. It is my hope that both Ruby and this book will serve to make your programming easy and enjoyable. Have fun! Yukihiro Matsumoto, a.k.a. ``Matz'' Japan, October 2000 Contents ^ Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide" Copyright © 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0. This reference is available for download.
http://www.rubycentral.com/book/foreword.html (2 of 2) [7/20/2001 9:48:33 PM]
Next >
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Preface This book is a tutorial and reference for the Ruby programming language. Use Ruby, and you'll write better code, be more productive, and enjoy programming more. These are bold claims, but we think that after reading this book you'll agree with them. And we have the experience to back up this belief. As Pragmatic Programmers we've tried many, many languages in our search for tools to make our lives easier, for tools to help us do our jobs better. Until now, though, we'd always been frustrated by the languages we were using. Our job is to solve problems, not spoonfeed compilers, so we like dynamic languages that adapt to us, without arbitrary, rigid rules. We need clarity so we can communicate using our code. We value conciseness and the ability to express a requirement in code accurately and efficiently. The less code we write, the less that can go wrong. (And our wrists and fingers are thankful, too.) We want to be as productive as possible, so we want our code to run the first time; time spent in the debugger is time stolen from the development clock. It also helps if we can try out code as we edit it; if you have to wait for a 2-hour make cycle, you may as well be using punch cards and submitting your work for batch compilation. We want a language that works at a high level of abstraction. The higher level the language, the less time we spend translating our requirements into code. When we discovered Ruby, we realized that we'd found what we'd been looking for. More than any other language with which we have worked, Ruby stays out of your way. You can concentrate on solving the problem at hand, instead of struggling with compiler and language issues. That's how it can help you become a better programmer: by giving you the chance to spend your time creating solutions for your users, not for the compiler.
Ruby Sparkles Take a true object-oriented language, such as Smalltalk. Drop the unfamiliar syntax and move to more conventional, file-based source code. Now add in a good measure of the flexibility and convenience of languages such as Python and Perl. You end up with Ruby. http://www.rubycentral.com/book/preface.html (1 of 8) [7/20/2001 9:48:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
OO aficionados will find much to like in Ruby: things such as pure object orientation (everything's an object), metaclasses, closures, iterators, and ubiquitous heterogeneous collections. Smalltalk users will feel right at home (and C++ and Java users will feel jealous). At the same time, Perl and Python wizards will find many of their favorite features: full regular expression support, tight integration with the underlying operating system, convenient shortcuts, and dynamic evaluation. Ruby is easy to learn. Everyday tasks are simple to code, and once you've done them, they are easy to maintain and grow. Apparently difficult things often turn out not to have been difficult after all. Ruby follows the Principle of Least Surprise---things work the way you would expect them to, with very few special cases or exceptions. And that really does make a difference when you're programming. We call Ruby a transparent language. By that we mean that Ruby doesn't obscure the solutions you write behind lots of syntax and the need to churn out reams of support code just to get simple things done. With Ruby you write programs close to the problem domain. Rather than constantly mapping your ideas and designs down to the pedestrian level of most languages, with Ruby you'll find you can express them directly and express them elegantly. This means you code faster. It also means your programs stay readable and maintainable. Using Ruby, we are constantly amazed at how much code we can write in one sitting, code that works the first time. There are very few syntax errors, no type violations, and far fewer bugs than usual. This makes sense: there's less to get wrong. No bothersome semicolons to type mechanically at the end of each line. No troublesome type declarations to keep in sync (especially in separate files). No unnecessary words just to keep the compiler happy. No error-prone framework code. So why learn Ruby? Because we think it will help you program better. It will help you to focus on the problem at hand, with fewer distractions. It will make your life easier.
What Kind of Language Is Ruby? In the old days, the distinction between languages was simple: they were either compiled, like C or Fortran, or interpreted, like BASIC. Compiled languages gave you speed and low-level access; interpreted languages were higher-level but slower. Times change, and things aren't that simple anymore. Some language designers have taken to calling their creations ``scripting languages.'' By this, we guess they mean that their languages are interpreted and can be used to replace batch files and shell scripts, orchestrating the behavior of other programs and the underlying operating system. Perl, TCL, and Python have all been called scripting languages. What exactly is a scripting language? Frankly we don't know if it's a distinction worth making. In Ruby, you can access all the underlying operating system features. You can do the same stuff in Ruby that you can in Perl or Python, and you can do it more cleanly. But Ruby is fundamentally different. It is a true programming language, too, with strong theoretical roots and an elegant, lightweight syntax. You could hack together a mess of ``scripts'' with Ruby, but you probably won't. Instead, you'll be more inclined to engineer a solution, to produce a program that is easy to understand, simple to maintain, and a piece of cake to extend and reuse in the future. Although we have used Ruby for scripting jobs, most of the time we use it as a general-purpose programming language. We've used it to write GUI applications and middle-tier server processes, and we're using it to format large parts of this book. Others have used it for managing server machines and http://www.rubycentral.com/book/preface.html (2 of 8) [7/20/2001 9:48:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
databases. Ruby is serving Web pages, interfacing to databases and generating dynamic content. People are writing artificial intelligence and machine learning programs in Ruby, and at least one person is using it to investigate natural evolution. Ruby's finding a home as a vehicle for exploratory mathematics. And people all over the world are using it as a way of gluing together all their different applications. It truly is a great language for producing solutions in a wide variety of problem domains.
Is Ruby for Me? Ruby is not the universal panacea for programmers' problems. There will always be times when you'll need a particular language: the environment may dictate it, you may have special libraries you need, performance concerns, or simply an issue with training. We haven't given up languages such as Java and C++ entirely (although there are times when we wish we could). However, Ruby is probably more applicable than you might think. It is easy to extend, both from within the language and by linking in third-party libraries. It is portable across a number of platforms. It's relatively lightweight and consumes only modest system resources. And it's easy to learn; we've known people who've put Ruby code into production systems within a day of picking up drafts of this book. We've used Ruby to implement parts of an X11 window manager, a task that's normally considered severe C coding. Ruby excelled, and helped us write code in hours that would otherwise have taken days. Once you get comfortable with Ruby, we think you'll keep coming back to it as your language of choice.
Why Did We Write This Book? So we'd just finished writing The Pragmatic Programmer, our families had just started talking to us again, and suddenly we felt the need to write another book. Why? We guess it comes down to a kind of missionary zeal. Ruby was created by Yukihiro Matsumoto (Matz) in Japan. Since 1995, its popularity in Japan has grown at an astounding rate; there are rumors that Ruby is more popular than Python in Japan. But to date, much of the detailed Ruby documentation is in Japanese. It probably isn't a programming language you'd just stumble across. We wanted to spread the word, to have more people outside Japan using Ruby and enjoying the benefits, so we decided to document Ruby in English. And what started out as a small project just sort of grew....
Ruby Versions This book documents Version 1.6 of Ruby, which was released in September 2000. Ruby version numbering follows the same scheme used for many other open source projects. Releases with even subversion numbers (1.0, 1.2, 1.4, and so on) are stable, public releases. These are the releases that are prepackaged and made available on the various Ruby Web sites. Development versions of the software have odd subversion numbers, such as 1.1 and 1.3. These you'll have to download and build for yourself, as described in the box on page xxvii.
http://www.rubycentral.com/book/preface.html (3 of 8) [7/20/2001 9:48:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Installing Ruby You can get Ruby from ftp://ftp.netlab.co.jp/pub/lang/ruby, or from the mirror sites listed on page 532 in Appendix C. There you will find the latest stable release, as well as various development releases. You'll always find source code releases of Ruby; you may also find prebuilt binaries for Windows or other operating systems (like the binary distribution of Ruby for Windows at http://www.pragmaticprogrammer.com/ruby/downloads/ruby-install.html). Sidebar: The Very Latest Ruby For those who just have to be on the very latest, hot-off-the-press and untested cutting edge (as we were while writing this book), you can get development versions straight from the developers' working repository. The Ruby developers use CVS (Concurrent Version System, freely available from http://www.cvshome.com) as their revision control system. You can check files out as an anonymous user from their archive by executing the following CVS commands: % cvs -d :pserver:
[email protected]:/home/cvs login (Logging in to
[email protected]) CVS password: guest % cvs -d :pserver:
[email protected]:/home/cvs checkout ruby The complete source code tree, just as the developers last left it, will now be copied to a ``ruby'' subdirectory on your machine, updating your local source tree from a repository on the other side of the world. Isn't it a great time to be alive?
Building Ruby In the Ruby distribution you'll find a file named README, which explains the installation procedure in detail. To summarize, you build Ruby on POSIX-based systems using the same four commands you use for most other open source applications: ./configure, make, make test, and make install. You can build Ruby under other environments (including Windows) by using a POSIX emulation environment such as cygwin[See http://sourceware.cygnus.com/cygwin for details.] or by using native compilers---see ``ntsetup.bat'' in the distribution's win32 subdirectory as a starting point.
Running Ruby Now that Ruby is installed, you'd probably like to run some programs. Unlike compiled environments, there are two ways to run Ruby---interactively and as a program.
http://www.rubycentral.com/book/preface.html (4 of 8) [7/20/2001 9:48:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Interactive Ruby The easiest way to run Ruby interactively is simply to type ``ruby'' at the shell prompt. % ruby puts "Hello, world!" ^D Hello, world! Here we typed in the single puts expression and an end of file character (which is control-D on our system). This process works, but it's sort of painful if you make a typo, and you can't really see what's going on as you type. In the sample directory in the Ruby distribution you'll find a script named ``eval.rb''. It goes one step better by showing us the value of each expression as it is entered: % cd sample % ruby eval.rb ruby> a = "Hello, world!" "Hello, world!" ruby> puts a Hello, world! nil ruby> ^D % Here we can see the output from puts, and then the return value from puts (which is nil). That's all fine and well, except that multiline expressions do not work, and you can't edit the line you're on, or go back and use previous lines (as you might with command history in a shell). For the next step up from eval.rb, we have irb---Interactive Ruby. irb is a Ruby Shell, complete with command-line history, line editing capabilities, and job control. It is quite configurable and has many options, so much so that it has its own appendix beginning on page 523. We recommend that you get familiar with irb so you can try some of our examples interactively.
Ruby Programs Finally, you can run a Ruby program from a file as you would any other shell script, Perl program, or Python program. You can simply run Ruby giving the script name as an argument: % ruby myprog.rb Or you can use the Unix ``shebang'' notation as the first line of the program file.[If your system supports it, you can avoid hard-coding the path to Ruby in the shebang line by using #!/usr/bin/env ruby, which will search your path for ruby and then execute it.]
http://www.rubycentral.com/book/preface.html (5 of 8) [7/20/2001 9:48:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
#!/usr/local/bin/ruby -w puts "Hello, World!" If you make this source file executable (using, for instance, chmod +x myprog.rb), Unix lets you run the file as a program: % ./myprog.rb Hello, World! You can do something similar under Microsoft Windows using file associations.
Resources Visit the Ruby Web sites, http://www.rubycentral.com and http://www.ruby-lang.org, to see what's new, and chat with other Ruby users on the newsgroup or mailing lists (see Appendix C). And we'd certainly appreciate hearing from you. Comments, suggestions, errors in the text, and problems in the examples are all welcome. E-mail us at: mailto:
[email protected] If you tell us about errors in the book, we'll add them to the errata list at: http://www.pragmaticprogrammer.com/ruby/errata/errata.html Finally, http://www.pragmaticprogrammer.com/ruby also contains the source code for almost all the book's examples, organized by page.
Acknowledgments A book is a massive undertaking, one that we would never be able to complete without help from our all our friends, old and new. We're proud to count among our old friends the team at Addison-Wesley: Mike Hendrickson, John Fuller, the ever-helpful Julie Steele, and the wonderful Julie DiNicola. Thank you all. Our reviewers were fantastic. We put them up against some incredibly tight deadlines and they came through for us. Reviewing a book full of technical detail isn't easy, so we're especially grateful to George Coe, Bob Davison, Jeff Deifik, Hal Fulton, Tadayoshi Funaba, Clemens Hintze, Kazuhiro Hiwada, Kikutani Makoto, Mike Linksvayer, Aleksi Niemelä, Lew Perin, Jared Richardson, Armin Roehrl, Conrad Schneiker, Patrick Schoenbach, and Eric Vought. Thanks also go to the two Julies at Addison-Wesley for coordinating this truly international effort. Several people helped us with specific areas of this book. Tadayoshi Funaba exchanged countless e-mails with us until we finally understood the Date module. Guy Decoux and Clemens Hintze patiently answered our questions about writing Ruby extensions, and Masaki Suketa helped us understand the WinOLE module. Although much of the original Ruby documentation is in Japanese, there is a growing body of English http://www.rubycentral.com/book/preface.html (6 of 8) [7/20/2001 9:48:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
translations, mostly undertaken by Japanese developers whose skills with English never cease to amaze us. Although there are too many individual contributions to this effort to name each author, we would like to single out Goto Kentaro, who has produced a large volume of high-quality documentation and placed it online. Finally, we have to thank Yukihiro ``Matz'' Matsumoto, the creator of Ruby. We've lost count of the number of questions we've asked of him, and the number of patient and detailed answers he's sent back. As well as creating a truly wonderful language, Matz has fostered a wonderfully supportive and open culture in which that language can prosper. Thank you all. Domo arigato gozaimasu. Dave Thomas and Andy Hunt THE PRAGMATIC PROGRAMMERS http://www.pragmaticprogrammer.com
Notation Conventions Throughout this book, we use the following typographic notations. Literal code examples are shown using a typewriter-like font: class SampleCode def run #... end end Within the text, Fred#doIt is a reference to an instance method (doIt) of class Fred, while Fred#new[In some other Ruby documentation, you may see class methods written as Fred::new. This is perfectly valid Ruby syntax; we just happen to feel that Fred.new is less distracting to read.] is a class method, and Fred#EOF is a class constant. The book contains many snippets of Ruby code. Where possible, we've tried to show what happens when they run. In simple cases, we show the value of expressions on the same line as the expression. For example: a = 1 b = 2 a + b
»
3
At times, we're also interested in the values of assignment statements, in which case we'll show them. a = 1 b = 2 a + b
» » »
1 2 3
If the program produces more complex output, we show it below the program code:
http://www.rubycentral.com/book/preface.html (7 of 8) [7/20/2001 9:48:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
3.times { puts "Hello!" } produces: Hello! Hello! Hello! In some of the library documentation, we wanted to show where spaces appear in the output. You'll see these spaces as `` '' characters. Command-line invocations are shown with literal text in a Roman font, and parameters you supply in an italic font. Optional elements are shown in large square brackets. ruby [flags]* [progname] [arguments]+
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Roadmap The main text of this book has four separate parts, each with its own personality, and each addressing different aspects of the Ruby language. In Part I, Facets of Ruby, you'll find a Ruby tutorial. It starts off with a short chapter on some of the terminology and concepts that are unique to Ruby. This chapter also includes enough basic syntax so that the other chapters will make sense. The rest of the tutorial is a top-down look at the language. There we talk about classes and objects, types, expressions, and all the other things that make up the language. We even end with a short chapter on digging yourself out when trouble strikes. One of the great things about Ruby is how well it integrates with its environment. Part II, Ruby in Its Setting, investigates this. Here you'll find practical information on running Ruby, and using Ruby with the Web. You'll learn how to create GUI applications using Tk, and how to use Ruby in a Microsoft Windows environment, including wonderful things such as making native API calls, COM integration, and Windows Automation. And you'll discover just how easy it is to extend Ruby and to embed Ruby within your own code. Part III, Ruby Crystallized, contains more advanced material. Here you'll find all the gory details about the language, the metaclass model, tainting, reflection, and marshaling. You could probably speed-read this the first time through, but we found ourselves using the tables in this section even as we were writing the rest of the book. The Ruby Library Reference is Part IV. It's big. We document over 800 methods in more than 40 built-in classes and modules. On top of that, we have another 70 pages describing some of the more useful library modules that come with Ruby. So, how should you read this book? Well, it depends on you. Depending on your level of expertise with programming in general, and OO in particular, you may want to read just a few portions of the book to start with. Here are our recommendations. If you're a beginner, you may want to start with the tutorial material in Part I. Keep the library reference close at hand as you start to write programs. Get familiar with the basic classes such as Array, Hash, and String. As you become more comfortable in the environment, you may want to investigate some of the more advanced topics in Part III. If you're already comfortable with Perl, Python, Java, or Smalltalk, then we'd suggest reading the introduction in Chapter 2 first. From there, you may want to take the slower approach and keep going http://www.rubycentral.com/book/roadmap.html (1 of 2) [7/20/2001 9:48:36 PM]
Programming Ruby: The Pragmatic Programmer's Guide
with the tutorial that follows, or skip ahead to the gritty details starting in Part III, followed by the library reference in Part IV. Experts, gurus, and ``I-don't-need-no-stinking-tutorial'' types can dive straight into the language reference in Chapter 18, which begins on page 201, skim the library reference, then use the book as a (rather attractive) coffee coaster. Of course, there's nothing wrong with just starting at the beginning and working your way through. And don't forget, if you run into a problem that you can't figure out, help is available. See Appendix C beginning on page 531 for more information. Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Ruby.new When we originally wrote this book, we had a grand plan (we were younger then). We wanted to document the language from the top down, starting with classes and objects, and ending with the nitty-gritty syntax details. It seemed like a good idea at the time. After all, most everything in Ruby is an object, so it made sense to talk about objects first. Or so we thought. Unfortunately, it turns out to be difficult to describe a language that way. If you haven't covered strings, if statements, assignments, and other details, it's difficult to write examples of classes. Throughout our top-down description, we kept coming across low-level details we needed to cover so that the example code would make sense. So, we came up with another grand plan (they don't call us pragmatic for nothing). We'd still describe Ruby starting at the top. But before we did that, we'd add a short chapter that described all the common language features used in the examples along with the special vocabulary used in Ruby, a kind of minitutorial to bootstrap us into the rest of the book.
Ruby Is an Object-Oriented Language Let's say it again. Ruby is a genuine object-oriented language. Everything you manipulate is an object, and the results of those manipulations are themselves objects. However, many languages make the same claim, and they often have a different interpretation of what object-oriented means and a different terminology for the concepts they employ. So, before we get too far into the details, let's briefly look at the terms and notation that we'll be using. When you write object-oriented code, you're normally looking to model concepts from the real world in your code. Typically during this modeling process you'll discover categories of things that need to be represented in code. In a jukebox, the concept of a ``song'' might be such a category. In Ruby, you'd define a class to represent each of these entities. A class is a combination of state (for example, the name of the song) and methods that use that state (perhaps a method to play the song). Once you have these classes, you'll typically want to create a number of instances of each. For the jukebox system containing a class called Song, you'd have separate instances for popular hits such as ``Ruby Tuesday,'' ``Enveloped in Python,'' ``String of Pearls,'' ``Small talk,'' and so on. The word object http://www.rubycentral.com/book/intro.html (1 of 12) [7/20/2001 9:48:38 PM]
Programming Ruby: The Pragmatic Programmer's Guide
is used interchangeably with class instance (and being lazy typists, we'll probably be using the word ``object'' more frequently). In Ruby, these objects are created by calling a constructor, a special method associated with a class. The standard constructor is called new. song1 = Song.new("Ruby Tuesday") song2 = Song.new("Enveloped in Python") # and so on These instances are both derived from the same class, but they have unique characteristics. First, every object has a unique object identifier (abbreviated as object id). Second, you can define instance variables, variables with values that are unique to each instance. These instance variables hold an object's state. Each of our songs, for example, will probably have an instance variable that holds the song title. Within each class, you can define instance methods. Each method is a chunk of functionality which may be called from within the class and (depending on accessibility constraints) from outside. These instance methods in turn have access to the object's instance variables, and hence to the object's state. Methods are invoked by sending a message to an object. The message contains the method's name, along with any parameters the method may need.[This idea of expressing method calls in the form of messages comes from Smalltalk.] When an object receives a message, it looks into its own class for a corresponding method. If found, that method is executed. If the method isn't found, ... well, we'll get to that later. This business of methods and messages may sound complicated, but in practice it is very natural. Let's look at some method calls. (Remember that the arrows in the code examples show the values returned by the corresponding expressions.) "gin joint".length "Rick".index("c") -1942.abs sam.play(aSong)
» » » »
9 2 1942 "duh dum, da dum de dum ..."
Here, the thing before the period is called the receiver, and the name after the period is the method to be invoked. The first example asks a string for its length, and the second asks a different string to find the index of the letter ``c.'' The third line has a number calculate its absolute value. Finally, we ask Sam to play us a song. It's worth noting here a major difference between Ruby and most other languages. In (say) Java, you'd find the absolute value of some number by calling a separate function and passing in that number. You might write number = Math.abs(number)
// Java code
In Ruby, the ability to determine an absolute value is built into numbers---they take care of the details internally. You simply send the message abs to a number object and let it do the work. number = number.abs
http://www.rubycentral.com/book/intro.html (2 of 12) [7/20/2001 9:48:38 PM]
Programming Ruby: The Pragmatic Programmer's Guide
The same applies to all Ruby objects: in C you'd write strlen(name), while in Ruby it's name.length, and so on. This is part of what we mean when we say that Ruby is a genuine OO language.
Some Basic Ruby Not many people like to read heaps of boring syntax rules when they're picking up a new language. So we're going to cheat. In this section we'll hit some of the highlights, the stuff you'll just have to know if you're going to write Ruby programs. Later, in Chapter 18, which begins on page 201, we'll go into all the gory details. Let's start off with a simple Ruby program. We'll write a method that returns a string, adding to that string a person's name. We'll then invoke that method a couple of times. def sayGoodnight(name) result = "Goodnight, " + name return result end # Time for bed... puts sayGoodnight("John-Boy") puts sayGoodnight("Mary-Ellen") First, some general observations. Ruby syntax is clean. You don't need semicolons at the ends of statements as long as you put each statement on a separate line. Ruby comments start with a # character and run to the end of the line. Code layout is pretty much up to you; indentation is not significant. Methods are defined with the keyword def, followed by the method name (in this case, ``sayGoodnight'') and the method's parameters between parentheses. Ruby doesn't use braces to delimit the bodies of compound statements and definitions. Instead, you simply finish the body with the keyword end. Our method's body is pretty simple. The first line concatenates the literal string ``Goodnight, '' to the parameter name and assigns the result to the local variable result. The next line returns that result to the caller. Note that we didn't have to declare the variable result; it sprang into existence when we assigned to it. Having defined the method, we call it twice. In both cases we pass the result to the method puts, which simply outputs its argument followed by a newline. Goodnight, John-Boy Goodnight, Mary-Ellen The line ``puts sayGoodnight("John-Boy")'' contains two method calls, one to sayGoodnight and the other to puts. Why does one call have its arguments in parentheses while the other doesn't? In this case it's purely a matter of taste. The following lines are all equivalent.
http://www.rubycentral.com/book/intro.html (3 of 12) [7/20/2001 9:48:38 PM]
Programming Ruby: The Pragmatic Programmer's Guide
puts sayGoodnight "John-Boy" puts sayGoodnight("John-Boy") puts(sayGoodnight "John-Boy") puts(sayGoodnight("John-Boy")) However, life isn't always that simple, and precedence rules can make it difficult to know which argument goes with which method invocation, so we recommend using parentheses in all but the simplest cases. This example also shows some Ruby string objects. There are many ways to create a string object, but probably the most common is to use string literals: sequences of characters between single or double quotation marks. The difference between the two forms is the amount of processing Ruby does on the string while constructing the literal. In the single-quoted case, Ruby does very little. With a few exceptions, what you type into the string literal becomes the string's value. In the double-quoted case, Ruby does more work. First, it looks for substitutions---sequences that start with a backslash character---and replaces them with some binary value. The most common of these is ``\n'', which is replaced with a newline character. When a string containing a newline is output, the ``\n'' forces a line break. puts "And Goodnight,\nGrandma" produces: And Goodnight, Grandma The second thing that Ruby does with double-quoted strings is expression interpolation. Within the string, the sequence #{expression} is replaced by the value of expression. We could use this to rewrite our previous method. def sayGoodnight(name) result = "Goodnight, #{name}" return result end When Ruby constructs this string object, it looks at the current value of name and substitutes it into the string. Arbitrarily complex expressions are allowed in the #{...} construct. As a shortcut, you don't need to supply the braces when the expression is simply a global, instance, or class variable. For more information on strings, as well as on the other Ruby standard types, see Chapter 5, which begins on page 49. Finally, we could simplify this method some more. The value returned by a Ruby method is the value of the last expression evaluated, so we can get rid of the return statement altogether.
http://www.rubycentral.com/book/intro.html (4 of 12) [7/20/2001 9:48:38 PM]
Programming Ruby: The Pragmatic Programmer's Guide
def sayGoodnight(name) "Goodnight, #{name}" end We promised that this section would be brief. We've got just one more topic to cover: Ruby names. For brevity, we'll be using some terms (such as class variable) that we aren't going to define here. However, by talking about the rules now, you'll be ahead of the game when we actually come to discuss instance variables and the like later. Ruby uses a convention to help it distinguish the usage of a name: the first characters of a name indicate how the name is used. Local variables, method parameters, and method names should all start with a lowercase letter or with an underscore. Global variables are prefixed with a dollar sign ($), while instance variables begin with an ``at'' sign (@). Class variables start with two ``at'' signs (@@). Finally, class names, module names, and constants should start with an uppercase letter. Samples of different names are given in Table 2.1 on page 10. Following this initial character, a name can be any combination of letters, digits, and underscores (with the proviso that the character following an @ sign may not be a digit). Example variable and class names Variables Local name fishAndChips x_axis thx1138 _26
Constants and Global Instance Class Class Names $debug @name @@total PI $CUSTOMER @point_1 @@symtab FeetPerMile $_ @X @@N String $plan9 @_ @@x_pos MyClass $Global @plan9 @@SINGLE Jazz_Song
Arrays and Hashes Ruby's arrays and hashes are indexed collections. Both store collections of objects, accessible using a key. With arrays, the key is an integer, whereas hashes support any object as a key. Both arrays and hashes grow as needed to hold new elements. It's more efficient to access array elements, but hashes provide more flexibility. Any particular array or hash can hold objects of differing types; you can have an array containing an integer, a string, and a floating point number, as we'll see in a minute. You can create and initialize a new array using an array literal---a set of elements between square brackets. Given an array object, you can access individual elements by supplying an index between square brackets, as the next example shows. a = [ 1, 'cat', 3.14 ] # array with three elements # access the first element » 1 a[0] # set the third element http://www.rubycentral.com/book/intro.html (5 of 12) [7/20/2001 9:48:38 PM]
Programming Ruby: The Pragmatic Programmer's Guide
a[2] = nil # dump out the array » [1, "cat", nil] a You can create empty arrays either by using an array literal with no elements or by using the array object's constructor, Array::new. empty1 = [] empty2 = Array.new Sometimes creating arrays of words can be a pain, what with all the quotes and commas. Fortunately, there's a shortcut: %w does just what we want. a = %w{ ant bee cat dog elk } » a[0] "ant" » a[3] "dog" Ruby hashes are similar to arrays. A hash literal uses braces rather than square brackets. The literal must supply two objects for every entry: one for the key, the other for the value. For example, you might want to map musical instruments to their orchestral sections. You could do this with a hash. instSection = 'cello' 'clarinet' 'drum' 'oboe' 'trumpet' 'violin' }
{ => => => => => =>
'string', 'woodwind', 'percussion', 'woodwind', 'brass', 'string'
Hashes are indexed using the same square bracket notation as arrays. instSection['oboe'] instSection['cello'] instSection['bassoon']
» "woodwind" » "string" » nil
As the last example shows, a hash by default returns nil when indexed by a key it doesn't contain. Normally this is convenient, as nil means false when used in conditional expressions. Sometimes you'll want to change this default. For example, if you're using a hash to count the number of times each key occurs, it's convenient to have the default value be zero. This is easily done by specifying a default value when you create a new, empty hash. histogram = Hash.new(0) histogram['key1'] histogram['key1'] = histogram['key1'] + 1
http://www.rubycentral.com/book/intro.html (6 of 12) [7/20/2001 9:48:38 PM]
»
0
Programming Ruby: The Pragmatic Programmer's Guide
histogram['key1']
»
1
Array and hash objects have lots of useful methods: see the discussion starting on page 35, and the reference sections starting on pages 282 and 321, for details.
Control Structures Ruby has all the usual control structures, such as if statements and while loops. Java, C, and Perl programmers may well get caught by the lack of braces around the bodies of these statements. Instead, Ruby uses the keyword end to signify the end of a body. if count > 10 puts "Try again" elsif tries == 3 puts "You lose" else puts "Enter a number" end Similarly, while statements are terminated with end. while weight < 100 and numPallets other.balance end end Because the attribute balance is protected, it's available only within Account objects.
Variables Now that we've gone to the trouble to create all these objects, let's make sure we don't lose them. Variables are used to keep track of objects; each variable holds a reference to an object. Figure not available... Let's confirm this with some code. person = "Tim" person.id person.type person
» » »
537684980 String "Tim"
On the first line, Ruby creates a new String object with the value ``Tim.'' A reference to this object is placed in the local variable person. A quick check shows that the variable has indeed taken on the personality of a string, with an object id, a type, and a value. So, is a variable an object? In Ruby, the answer is ``no.'' A variable is simply a reference to an object. Objects float around in a big pool somewhere (the heap, most of the time) and are pointed to by variables.
http://www.rubycentral.com/book/tut_classes.html (10 of 11) [7/20/2001 9:48:40 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Let's make the example slightly more complicated. person1 = "Tim" person2 = person1 person1[0] = 'J' » »
person1 person2
"Jim" "Jim"
What happened here? We changed the first character of person1, but both person1 and person2 changed from ``Tim'' to ``Jim.'' It all comes back to the fact that variables hold references to objects, not the objects themselves. The assignment of person1 to person2 doesn't create any new objects; it simply copies person1's object reference to person2, so that both person1 and person2 refer to the same object. We show this in Figure 3.1 on page 33. Assignment aliases objects, potentially giving you multiple variables that reference the same object. But can't this cause problems in your code? It can, but not as often as you'd think (objects in Java, for example, work exactly the same way). For instance, in the example in Figure 3.1, you could avoid aliasing by using the dup method of String, which creates a new String object with identical contents. person1 = "Tim" person2 = person1.dup person1[0] = "J" person1 person2
» »
"Jim" "Tim"
You can also prevent anyone from changing a particular object by freezing it (we talk more about freezing objects on page 255). Attempt to alter a frozen object, and Ruby will raise a TypeError exception. person1 = "Tim" person2 = person1 person1.freeze person2[0] = "J"
# prevent modifications to the object
produces: prog.rb:4:in `=': can't modify frozen string (TypeError) from prog.rb:4
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Contents ^
Next >
class Array
Parent: Object Version: 1.6
Index: [ ] new & * + -- anArray Returns a new array populated with the given objects. Equivalent to the operator form Array.[](...). Array.[]( 1, 'a', /^A/ ) Array[ 1, 'a', /^A/ ] [ 1, 'a', /^A/ ]
new
» [1, "a", /^A/] » [1, "a", /^A/] » [1, "a", /^A/]
Array.new( anInteger=0, anObject=nil ) -> anArray
http://www.rubycentral.com/book/ref_c_array.html (1 of 15) [7/20/2001 9:48:42 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns a new array, optionally with a size and initial value (that is, anInteger references to the same anObject). Array.new Array.new(2) Array.new(5, "A") Array.new(2, Hash.new)
» » » »
[] [nil, nil] ["A", "A", "A", "A", "A"] [{}, {}]
instance methods &
arr & anOtherArray -> anArray Set Intersection---Returns a new array containing elements common to the two arrays, with no duplicates. » [1, 3]
[ 1, 1, 3, 5 ] & [ 1, 2, 3 ]
*
arr * anInteger -> anArray arr * aString -> anOtherString Repetition---With a String argument, equivalent to arr.join(aString). Otherwise, returns a new array built by concatenating the anInteger copies of arr. [ 1, 2, 3 ] * 3
+
» [1, 2, 3, 1, 2, 3, 1, 2, 3]
arr + anOtherArray -> anArray Concatenation---Returns a new array built by concatenating the two arrays together to produce a third array. [ 1, 2, 3 ] + [ 4, 5 ]
--
» [1, 2, 3, 4, 5]
arr - anOtherArray -> anArray Set Difference---Returns a new array that is a copy of the original array, removing any items that also appear in anOtherArray and duplicated items. [ 1, 1, 2, 2, 3, 3, 3, 4, 5 ] - [ 1, 2, 4 ]
true or false Equality---Two arrays are equal if they contain the same number of elements and if each element is equal to (according to Object#==) the corresponding element in the other array. [ "a", "c" ] == [ "a", "c", 7 ] [ "a", "c", 7 ] == [ "a", "c", 7 ] [ "a", "c", 7 ] == [ "a", "d", "f" ]
===
» false » true » false
arr === anOtherArray -> true or false Case Equality---Equality as evaluated by case expressions. For arrays, this is the same as Array#==.
[]
arr[anInteger] -> anObject or nil arr[start, length] -> aSubArray or nil arr[aRange] -> aSubArray or nil Element Reference---Returns the element at index anInteger, or returns a subarray starting at index start and continuing for length elements, or returns a subarray specified by aRange. Negative indices count backward from the end of the array (-1 is the last element). Returns nil if any indices are out of range. a = [ "a", "b", "c", "d", "e" ] » "cab" a[2] + a[0] + a[1] » nil a[6] » ["b", "c"] a[1, 2] » ["b", "c", "d"] a[1..3] » ["e"] a[4..7] » nil a[6..10] » ["c", "d", "e"] a[-3, 3]
http://www.rubycentral.com/book/ref_c_array.html (3 of 15) [7/20/2001 9:48:42 PM]
Programming Ruby: The Pragmatic Programmer's Guide
[ ]=
arr[anInteger] = anObject -> anObject arr[start, length] = aSubArray -> aSubArray arr[aRange] = aSubArray -> aSubArray Element Assignment---Sets the element at index anInteger, or replaces a subarray starting at index start and continuing for length elements, or replaces a subarray specified by aRange. If anInteger is greater than the current capacity of the array, the array grows automatically. A negative anInteger will count backward from the end of the array. Inserts elements if length is zero. If subArray is nil, deletes elements from arr. An IndexError is raised if a negative index points past the beginning of the array. See also Array#push, Array#unshift. » [] a = Array.new a[4] = "4"; a » [nil, nil, nil, nil, "4"] a[0, 3] = [ 'a', 'b', 'c' ]; a » ["a", "b", "c", nil, "4"] a[1..2] = [ 1, 2 ]; a » ["a", 1, 2, nil, "4"] a[0, 2] = "?"; a » ["?", 2, nil, "4"] a[0..2] = "A"; a » ["A", "4"] a[-1] = "Z"; a » ["A", "Z"] a[1..-1] = nil; a » ["A"]
|
arr | anOtherArray -> anArray Set Union---Returns a new array by joining this array with anOtherArray, removing duplicates. [ "a", "b", "c" ] | [ "c", "d", "a" ] » ["a", "b", "c", "d"]
assoc
arr.assoc( anObject ) -> anArray or nil Searches through an array whose elements are also arrays comparing anObject with the first element of each contained array using anObject.== . Returns the first contained array that matches (that is, the first associated array), or nil if no match is found. See also Array#rassoc. s1 = [ "colors", "red", "blue", "green" ] s2 = [ "letters", "a", "b", "c" ] s3 = "foo" a = [ s1, s2, s3 ] » ["letters", "a", "b", "c"] a.assoc("letters") » nil a.assoc("foo")
at
arr.at( anInteger ) -> anObject or nil
http://www.rubycentral.com/book/ref_c_array.html (4 of 15) [7/20/2001 9:48:42 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns the element at index anInteger. A negative index counts from the end of arr. Returns nil if the index is out of range. See also Array#[]. (Array#at is slightly faster than Array#[], as it does not accept ranges and so on.) a = [ "a", "b", "c", "d", "e" ] a.at(0) a.at(-1)
clear
» »
"a" "e"
arr.clear -> arr Removes all elements from arr. a = [ "a", "b", "c", "d", "e" ] a.clear
collect
»
[]
arr.collect {| obj | block } -> anArray Returns a new array by invoking block once for every element, passing each element as a parameter to block. The result of block is used as the given element in the new array. See also Array#collect!. a = [ "a", "b", "c", "d" ] a.collect {|x| x + "!" } a
collect!
» ["a!", "b!", "c!", "d!"] » ["a", "b", "c", "d"]
arr.collect! {| obj | block } -> arr Invokes block once for each element of arr, replacing the element with the value returned by block. See also Array#collect. a = [ "a", "b", "c", "d" ] a.collect! {|x| x + "!" } a
compact
» ["a!", "b!", "c!", "d!"] » ["a!", "b!", "c!", "d!"]
arr.compact -> anArray Returns a new array based on the arr with all nil elements removed. [ "a", nil, "b", nil, "c", nil ].compact
compact!
arr.compact! -> arr or nil
http://www.rubycentral.com/book/ref_c_array.html (5 of 15) [7/20/2001 9:48:42 PM]
» ["a", "b", "c"]
Programming Ruby: The Pragmatic Programmer's Guide
Same as Array#compact, but modifies the receiver in place. Returns nil if no changes were made. [ "a", nil, "b", nil, "c" ].compact! [ "a", "b", "c" ].compact!
concat
» ["a", "b", "c"] » nil
arr.concat( anOtherArray ) -> arr Appends the elements in anOtherArray to arr. [ "a", "b" ].concat( ["c", "d"] )
delete
» ["a", "b", "c", "d"]
arr.delete( anObject ) -> anObject or nil arr.delete( anObject ) {| | block } -> anObject or nil Deletes items from the self that are equal to anObject. If the item is not found, returns nil. If the optional code block is given, returns the result of block if the item is not found. a = [ "a", "b", "b", "b", "c" ] a.delete("b") a a.delete("z") a.delete("z") { "not found" }
delete_at
» » » »
"b" ["a", "c"] nil "not found"
arr.delete_at( anIndex ) -> anObject or nil Deletes the element at the specified index, returning that element, or nil if the index is out of range. See also Array#slice!. a = %w( ant bat cat dog ) » "cat" a.delete_at(2) » ["ant", "bat", "dog"] a » nil a.delete_at(99)
delete_if
arr.delete_if {| | block } -> arr Deletes every element of arr for which block evaluates to true. a = [ "a", "b", "c" ] a.delete_if {|x| x >= "b" }
each
arr.each {| item | block } -> arr
http://www.rubycentral.com/book/ref_c_array.html (6 of 15) [7/20/2001 9:48:42 PM]
» ["a"]
Programming Ruby: The Pragmatic Programmer's Guide
Calls block once for each element in arr, passing that element as a parameter. a = [ "a", "b", "c" ] a.each {|x| print x, " -- " } produces: a -- b -- c --
each_index
arr.each_index {| anIndex | block } -> arr Same as Array#each, but passes the index of the element instead of the element itself. a = [ "a", "b", "c" ] a.each_index {|x| print x, " -- " } produces: 0 -- 1 -- 2 --
empty?
arr.empty? -> true or false Returns true if arr array contains no elements. [].empty?
eql?
»
true
arr.eql?( anOtherArray ) -> true or false An array is equal to another array if the lengths are equal and each corresponding element is equal (according to Object#eql?). See also Array#. eql? is used for Hash comparison. [ "a", "b", "c" ].eql?(["a", "b", "c"]) [ "a", "b", "c" ].eql?(["a", "b"]) [ "a", "b", "c" ].eql?(["b", "c", "d"])
fill
» true » false » false
arr.fill( anObject ) -> arr arr.fill( anObject, start [, length] ) -> arr arr.fill( anObject, aRange ) -> arr Sets the selected elements of arr (which may be the entire array) to anObject. A start of nil is equivalent to zero. A length of nil is equivalent to arr.length. a = [ "a", "b", "c", "d" ] » ["x", "x", "x", "x"] a.fill("x") » ["x", "x", "z", "z"] a.fill("z", 2, 2) » ["y", "y", "z", "z"] a.fill("y", 0..1)
http://www.rubycentral.com/book/ref_c_array.html (7 of 15) [7/20/2001 9:48:42 PM]
Programming Ruby: The Pragmatic Programmer's Guide
first
arr.first -> anObject or nil Returns the first element of the array. If the array is empty, returns nil. a = [ "q", "r", "s", "t" ] a.first
flatten
»
"q"
arr.flatten -> anArray Returns a new array that is a one-dimensional flattening of this array (recursively). That is, for every element that is an array, extract its elements into the new array. » [1, 2, 3] s = [ 1, 2, 3 ] t = [ 4, 5, 6, [7, 8] ] » [4, 5, 6, [7, 8]] » [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10] a = [ s, t, 9, 10 ] » [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a.flatten
flatten!
arr.flatten! -> arr or nil Same as Array#flatten, but modifies the receiver in place. Returns nil if no modifications were made (i.e., arr contains no subarrays.) a = [ 1, 2, [3, [4, 5] ] » a.flatten! » a.flatten! » a
include?
] [1, 2, 3, 4, 5] nil [1, 2, 3, 4, 5]
arr.include?( anObject ) -> true or false Returns true if the given object is present in arr (that is, if any object == anObject), false otherwise. a = [ "a", "b", "c" ] a.include?("b") a.include?("z") Template characters for Array#pack Directive @ A a B b C c d
Meaning Moves to absolute position ASCII string (space padded, count is width) ASCII string (null padded, count is width) Bit string (descending bit order) Bit string (ascending bit order) Unsigned char Char Double-precision float, native format
http://www.rubycentral.com/book/ref_c_array.html (8 of 15) [7/20/2001 9:48:42 PM]
» »
true false
Programming Ruby: The Pragmatic Programmer's Guide
E e f G g H h I i L l M m N n P p S s U u V v X x Z
index
Double-precision float, little-endian byte order Single-precision float, little-endian byte order Single-precision float, native format Double-precision float, network (big-endian) byte order Single-precision float, network (big-endian) byte order Hex string (high nibble first) Hex string (low nibble first) Unsigned integer Integer Unsigned long Long Quoted printable, MIME encoding (see RFC2045) Base64 encoded string Long, network (big-endian) byte order Short, network (big-endian) byte-order Pointer to a structure (fixed-length string) Pointer to a null-terminated string Unsigned short Short UTF-8 UU-encoded string Long, little-endian byte order Short, little-endian byte order Back up a byte Null byte Same as ``A''
arr.index( anObject ) -> anInteger or nil Returns the index of the first object in arr such that the object == anObject. Returns nil if no match is found. a = [ "a", "b", "c" ] a.index("b") a.index("z")
indexes
arr.indexes( i1, i2, ... iN ) -> anArray
http://www.rubycentral.com/book/ref_c_array.html (9 of 15) [7/20/2001 9:48:42 PM]
» »
1 nil
Programming Ruby: The Pragmatic Programmer's Guide
Returns a new array consisting of elements at the given indices. May insert nil for indices out of range. a = [ "a", "b", "c", "d", "e", "f", "g" ] » ["a", "c", "e"] a.indexes(0, 2, 4) » ["a", "c", "e", nil] a.indexes(0, 2, 4, 12)
indices
arr.indices( i1, i2, ... iN ) -> anArray Synonym for Array#indexes.
join
arr.join( aSepString=$, ) -> aString Returns a string created by converting each element of the array to a string, separated by aSepString. [ "a", "b", "c" ].join [ "a", "b", "c" ].join("-")
last
» "abc" » "a-b-c"
arr.last -> anObject or nil Returns the last element of arr. If the array is empty, returns nil. [ "w", "x", "y", "z" ].last
length
» "z"
arr.length -> anInteger Returns the number of elements in arr. May be zero. [ 1, 2, 3, 4, 5 ].length
map!
»
5
arr.map! {| obj | block } -> arr Synonym for Array#collect!.
nitems
arr.nitems -> anInteger Returns the number of non-nil elements in arr. May be zero. [ 1, nil, 3, nil, 5 ].nitems
pack
arr.pack ( aTemplateString ) -> aBinaryString
http://www.rubycentral.com/book/ref_c_array.html (10 of 15) [7/20/2001 9:48:42 PM]
» 3
Programming Ruby: The Pragmatic Programmer's Guide
Packs the contents of arr into a binary sequence according to the directives in aTemplateString (see Table 22.1 on page 289). Directives ``A,'' ``a,'' and ``Z'' may be followed by a count, which gives the width of the resulting field. The remaining directives also may take a count, indicating the number of array elements to convert. If the count is an asterisk (``*''), all remaining array elements will be converted. Any of the directives ``sSiIlL'' may be followed by an underscore (``_'') to use the underlying platform's native size for the specified type; otherwise, they use a platform-independent size. Spaces are ignored in the template string. See also String#unpack on page 383. a = [ "a", "b", "c" ] n = [ 65, 66, 67 ] » "a a.pack("A3A3A3") b c " » "a\000\000b\000\000c\000\000" a.pack("a3a3a3") » "ABC" n.pack("ccc")
pop
arr.pop -> anObject or nil
Removes the last element from arr and returns it, or nil if the array is empty (as with a stack). a = [ "a", "m", "z" ] » a.pop "z" » a ["a", "m"]
push
arr.push( [anObject]+ ) -> arr Appends the given argument(s) to the end of arr (as with a stack). a = [ "a", "b", "c" ] a.push("d", "e", "f")
rassoc
» ["a", "b", "c", "d", "e", "f"]
arr.rassoc( key ) -> anArray or nil Searches through the array whose elements are also arrays. Compares key with the second element of each contained array using ==. Returns the first contained array that matches. See also assoc. a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ] » [2, "two"] a.rassoc("two") » nil a.rassoc("four")
reject!
arr.reject! {| | block } -> arr or nil Equivalent to Array#delete_if, but returns nil if no changes were made.
http://www.rubycentral.com/book/ref_c_array.html (11 of 15) [7/20/2001 9:48:42 PM]
Programming Ruby: The Pragmatic Programmer's Guide
replace
arr.replace( anOtherArray ) -> arr Replaces the contents of arr with the contents of anOtherArray, truncating or expanding if necessary. a = [ "a", "b", "c", "d", "e" ] a.replace( [ "x", "y", "z" ] ) a
reverse
» ["x", "y", "z"] » ["x", "y", "z"]
arr.reverse -> anArray Returns a new array using arr's elements in reverse order. » ["c", "b", "a"] » [1]
[ "a", "b", "c" ].reverse [ 1 ].reverse
reverse!
arr.reverse! -> arr or nil Same as reverse, but returns nil if arr is unchanged (arr.length is zero or one). a = [ "a", "b", "c" ] a.reverse! a [ 1 ].reverse!
reverse_each
» ["c", "b", "a"] » ["c", "b", "a"] » nil
arr.reverse_each {| | block } Same as Array#each, but traverses arr in reverse order. a = [ "a", "b", "c" ] a.reverse_each {|x| print x, " " } produces: c b a
rindex
arr.rindex( anObject ) -> anInteger or nil Returns the index of the last object in arr such that the object == anObject. Returns nil if no match is found. a = [ "a", "b", "b", "b", "c" ] a.rindex("b") a.rindex("z")
arr.shift -> anObject or nil http://www.rubycentral.com/book/ref_c_array.html (12 of 15) [7/20/2001 9:48:42 PM]
» »
3 nil
Programming Ruby: The Pragmatic Programmer's Guide
shift Returns the first element of arr and removes it (shifting all other elements down by one). Returns nil if the array is empty. args = [ "-m", "-q", "filename" ] » "-m" args.shift » ["-q", "filename"] args
size
arr.size -> anInteger Synonym for Array#length.
slice
arr.slice( anInteger ) -> anObject arr.slice( start, length ) -> aSubArray arr.slice( aRange ) -> aSubArray Synonym for Array#[ ]. a = [ "a", "b", "c", "d", "e" ] a.slice(2) + a.slice(0) + a.slice(1) a.slice(6) a.slice(1, 2) a.slice(1..3) a.slice(4..7) a.slice(6..10) a.slice(-3, 3)
slice!
» » » » » » »
"cab" nil ["b", "c"] ["b", "c", "d"] ["e"] nil ["c", "d", "e"]
arr.slice!( anInteger ) -> anObject or nil arr.slice!( start, length ) -> aSubArray or nil arr.slice!( aRange ) -> aSubArray or nil Deletes the element(s) given by an index (optionally with a length) or by a range. Returns the deleted object, subarray, or nil if the index is out of range. Equivalent to: def slice!(*args) result = self[*args] self[*args] = nil result end a = [ "a", "b", "c" ] a.slice!(1) a a.slice!(-1) a a.slice!(100)
http://www.rubycentral.com/book/ref_c_array.html (13 of 15) [7/20/2001 9:48:42 PM]
» » » » »
"b" ["a", "c"] "c" ["a"] nil
Programming Ruby: The Pragmatic Programmer's Guide
a
sort
»
["a"]
arr.sort -> anArray arr.sort {| a,b | block } -> anArray Returns a new array created by sorting arr. Comparisons for the sort will be done using the operator or using an optional code block. The block implements a comparison between a and b, returning -1, 0, or +1. a = [ "d", "a", "e", "c", "b" ] » ["a", "b", "c", "d", "e"] a.sort » ["e", "d", "c", "b", "a"] a.sort {|x,y| y x }
sort!
arr.sort! -> arr arr.sort! {| a,b | block } -> arr Same as Array#sort, but modifies the receiver in place. arr is effectively frozen while a sort is in progress. a = [ "d", "a", "e", "c", "b" ] » ["a", "b", "c", "d", "e"] a.sort! » ["a", "b", "c", "d", "e"] a
to_a
arr.to_a -> arr Returns arr.
to_ary
arr.to_ary -> arr Synonym for Array#to_a.
to_s
arr.to_s -> aString Returns arr.join. [ "a", "e", "i", "o" ].to_s
uniq
arr.uniq -> anArray Returns a new array by removing duplicate values in arr. a = [ "a", "a", "b", "b", "c" ] » ["a", "b", "c"] a.uniq
http://www.rubycentral.com/book/ref_c_array.html (14 of 15) [7/20/2001 9:48:42 PM]
» "aeio"
Programming Ruby: The Pragmatic Programmer's Guide
uniq!
arr.uniq! -> arr or nil Same as Array#uniq, but modifies the receiver in place. Returns nil if no changes are made (that is, no duplicates are found). a = [ "a", "a", "b", "b", "c" ] » ["a", "b", "c"] a.uniq! b = [ "a", "b", "c" ] » nil b.uniq!
unshift
arr.unshift( anObject ) -> arr Prepends anObject to the front of arr, and shifts all other elements up one. a = [ "b", "c", "d" ] a.unshift("a")
» ["a", "b", "c", "d"]
Contents ^ Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide" Copyright © 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0. This reference is available for download.
http://www.rubycentral.com/book/ref_c_array.html (15 of 15) [7/20/2001 9:48:42 PM]
Next >
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Containers, Blocks, and Iterators A jukebox with one song is unlikely to be popular (except perhaps in some very, very scary bars), so pretty soon we'll have to start thinking about producing a catalog of available songs and a playlist of songs waiting to be played. Both of these are containers: objects that hold references to one or more other objects. Both the catalog and the playlist need a similar set of methods: add a song, remove a song, return a list of songs, and so on. The playlist may perform additional tasks, such as inserting advertising every so often or keeping track of cumulative play time, but we'll worry about these things later. In the meantime, it seems like a good idea to develop some kind of generic SongList class, which we can specialize into catalogs and playlists.
Containers Before we start implementing, we'll need to work out how to store the list of songs inside a SongList object. We have three obvious choices. We could use the Ruby Array type, use the Ruby Hash type, or create our own list structure. Being lazy, for now we'll look at arrays and hashes, and choose one of these for our class.
Arrays The class Array holds a collection of object references. Each object reference occupies a position in the array, identified by a non-negative integer index. You can create arrays using literals or by explicitly creating an Array object. A literal array is simply a list of objects between square brackets. a = [ 3.14159, "pie", 99 ] » Array a.type » 3 a.length » 3.14159 a[0] » "pie" a[1] » 99 a[2] » nil a[3] b = Array.new b.type b.length b[0] = "second" b[1] = "array" b
» Array » 0
» ["second", "array"]
Arrays are indexed using the [] operator. As with most Ruby operators, this is actually a method (in class Array) and hence can be overridden in subclasses. As the example shows, array indices start at zero. Index an array with a single integer, and it returns the object at that position or returns nil if nothing's there. Index an array with a negative integer, and it counts from the end. This is shown in Figure 4.1 on page 37. Figure not available... a = [ 1, 3, 5, 7, 9 ]
http://www.rubycentral.com/book/tut_containers.html (1 of 10) [7/20/2001 9:48:45 PM]
Programming Ruby: The Pragmatic Programmer's Guide
» » »
a[-1] a[-2] a[-99]
9 7 nil
You can also index arrays with a pair of numbers, [start, count]. This returns a new array consisting of references to count objects starting at position start. a = [ 1, 3, 5, 7, 9 ] a[1, 3] a[3, 1] a[-3, 2]
» » »
[3, 5, 7] [7] [5, 7]
Finally, you can index arrays using ranges, in which start and end positions are separated by two or three periods. The two-period form includes the end position, while the three-period form does not. a = [ 1, 3, 5, 7, 9 ] a[1..3] a[1...3] a[3..3] a[-3..-1]
» » » »
[3, 5, 7] [3, 5] [7] [5, 7, 9]
The [] operator has a corresponding []= operator, which lets you set elements in the array. If used with a single integer index, the element at that position is replaced by whatever is on the right-hand side of the assignment. Any gaps that result will be filled with nil. a = [ 1, 3, 5, 7, 9 ] a[1] = 'bat' a[-3] = 'cat' a[3] = [ 9, 8 ] a[6] = 99
» » » » »
[1, 3, 5, 7, 9] [1, "bat", 5, 7, 9] [1, "bat", "cat", 7, 9] [1, "bat", "cat", [9, 8], 9] [1, "bat", "cat", [9, 8], 9, nil, 99]
If the index to []= is two numbers (a start and a length) or a range, then those elements in the original array are replaced by whatever is on the right-hand side of the assignment. If the length is zero, the right-hand side is inserted into the array before the start position; no elements are removed. If the right-hand side is itself an array, its elements are used in the replacement. The array size is automatically adjusted if the index selects a different number of elements than are available on the right-hand side of the assignment. a = [ 1, 3, 5, 7, 9 ] a[2, 2] = 'cat' a[2, 0] = 'dog' a[1, 1] = [ 9, 8, 7 ] a[0..3] = [] a[5] = 99
» » » » » »
[1, 3, 5, 7, 9] [1, 3, "cat", 9] [1, 3, "dog", "cat", 9] [1, 9, 8, 7, "dog", "cat", 9] ["dog", "cat", 9] ["dog", "cat", 9, nil, nil, 99]
Arrays have a large number of other useful methods. Using these, you can treat arrays as stacks, sets, queues, dequeues, and fifos. A complete list of array methods starts on page 282.
Hashes Hashes (sometimes known as associative arrays or dictionaries) are similar to arrays, in that they are indexed collectives of object references. However, while you index arrays with integers, you can index a hash with objects of any type: strings, regular expressions, and so on. When you store a value in a hash, you actually supply two objects---the key and the value. You can subsequently retrieve the value by indexing the hash with the same key. The values in a hash can be any objects of any type. The example that follows uses hash literals: a list of key => value pairs between braces. h = { 'dog' => 'canine', 'cat' => 'feline', 'donkey' => 'asinine' } h.length h['dog'] h['cow'] h[12]
» 3 » "canine" = 'bovine' = 'dodecine'
http://www.rubycentral.com/book/tut_containers.html (2 of 10) [7/20/2001 9:48:45 PM]
Programming Ruby: The Pragmatic Programmer's Guide
h['cat'] = 99 » {"cow"=>"bovine", 12=>"dodecine", "dog"=>"canine", "donkey"=>"asinine", "cat"=>99} h Compared with arrays, hashes have one significant advantage: they can use any object as an index. However, they also have a significant disadvantage: their elements are not ordered, so you cannot easily use a hash as a stack or a queue. You'll find that hashes are one of the most commonly used data structures in Ruby. A full list of the methods implemented by class Hash starts on page 321.
Implementing a SongList Container After that little diversion into arrays and hashes, we're now ready to implement the jukebox's SongList. Let's invent a basic list of methods we need in our SongList. We'll want to add to it as we go along, but it will do for now. append( aSong ) » list Append the given song to the list. deleteFirst() » aSong Remove the first song from the list, returning that song. deleteLast() » aSong Remove the last song from the list, returning that song. [ anIndex } » aSong Return the song identified by anIndex, which may be an integer index or a song title. This list gives us a clue to the implementation. The ability to append songs at the end, and remove them from both the front and end, suggests a dequeue---a double-ended queue---which we know we can implement using an Array. Similarly, the ability to return a song at an integer position in the list is supported by arrays. However, there's also the need to be able to retrieve songs by title, which might suggest using a hash, with the title as a key and the song as a value. Could we use a hash? Well, possibly, but there are problems. First a hash is unordered, so we'd probably need to use an ancillary array to keep track of the list. A bigger problem is that a hash does not support multiple keys with the same value. That would be a problem for our playlist, where the same song might be queued up for playing multiple times. So, for now we'll stick with an array of songs, searching it for titles when needed. If this becomes a performance bottleneck, we can always add some kind of hash-based lookup later. We'll start our class with a basic initialize method, which creates the Array we'll use to hold the songs and stores a reference to it in the instance variable @songs. class SongList def initialize @songs = Array.new end end The SongList#append method adds the given song to the end of the @songs array. It also returns self, a reference to the current SongList object. This is a useful convention, as it lets us chain together multiple calls to append. We'll see an example of this later. class SongList def append(aSong) @songs.push(aSong) self end end Then we'll add the deleteFirst and deleteLast methods, trivially implemented using Array#shift and Array#pop, respectively.
http://www.rubycentral.com/book/tut_containers.html (3 of 10) [7/20/2001 9:48:45 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class SongList def deleteFirst @songs.shift end def deleteLast @songs.pop end end At this point, a quick test might be in order. First, we'll append four songs to the list. Just to show off, we'll use the fact that append returns the SongList object to chain together these method calls. list = SongList.new list. append(Song.new('title1', append(Song.new('title2', append(Song.new('title3', append(Song.new('title4',
'artist1', 'artist2', 'artist3', 'artist4',
1)). 2)). 3)). 4))
Then we'll check that songs are taken from the start and end of the list correctly, and that nil is returned when the list becomes empty. list.deleteFirst list.deleteFirst list.deleteLast list.deleteLast list.deleteLast
» » » » »
Song: Song: Song: Song: nil
title1--artist1 title2--artist2 title4--artist4 title3--artist3
(1) (2) (4) (3)
So far so good. Our next method is [], which accesses elements by index. If the index is a number (which we check using Object#kind_of?), we just return the element at that position. class SongList def [](key) if key.kind_of?(Integer) @songs[key] else # ... end end end Again, testing this is pretty trivial. list[0] list[2] list[9]
» Song: title1--artist1 (1) » Song: title3--artist3 (3) » nil
Now we need to add the facility that lets us look up a song by title. This is going to involve scanning through the songs in the list, checking the title of each. To do this, we first need to spend a couple of pages looking at one of Ruby's neatest features: iterators.
Blocks and Iterators So, our next problem with SongList is to implement the code in method [] that takes a string and searches for a song with that title. This seems straightforward: we have an array of songs, so we just go through it one element at a time, looking for a match.
http://www.rubycentral.com/book/tut_containers.html (4 of 10) [7/20/2001 9:48:45 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class SongList def [](key) if key.kind_of?(Integer) return @songs[key] else for i in
[email protected] return @songs[i] if key == @songs[i].name end end return nil end end This works, and it looks comfortingly familiar: a for loop iterating over an array. What could be more natural? It turns out there is something more natural. In a way, our for loop is somewhat too intimate with the array; it asks for a length, then retrieves values in turn until it finds a match. Why not just ask the array to apply a test to each of its members? That's just what the find method in Array does. class SongList def [](key) if key.kind_of?(Integer) result = @songs[key] else result = @songs.find { |aSong| key == aSong.name } end return result end end We could use if as a statement modifier to shorten the code even more. class SongList def [](key) return @songs[key] if key.kind_of?(Integer) return @songs.find { |aSong| aSong.name == key } end end The method find is an iterator---a method that invokes a block of code repeatedly. Iterators and code blocks are among the more interesting features of Ruby, so let's spend a while looking into them (and in the process we'll find out exactly what that line of code in our [] method actually does).
Implementing Iterators A Ruby iterator is simply a method that can invoke a block of code. At first sight, a block in Ruby looks just like a block in C, Java, or Perl. Unfortunately, in this case looks are deceiving---a Ruby block is a way of grouping statements, but not in the conventional way. First, a block may appear only in the source adjacent to a method call; the block is written starting on the same line as the method's last parameter. Second, the code in the block is not executed at the time it is encountered. Instead, Ruby remembers the context in which the block appears (the local variables, the current object, and so on), and then enters the method. This is where the magic starts. Within the method, the block may be invoked, almost as if it were a method itself, using the yield statement. Whenever a yield is executed, it invokes the code in the block. When the block exits, control picks back up immediately after the yield.[Programming-language buffs will be pleased to know that the keyword yield was chosen to echo the yield function in Liskov's language CLU, a language that is over 20 years old and yet contains features that still haven't been widely exploited by the CLU-less.] Let's start with a trivial example.
http://www.rubycentral.com/book/tut_containers.html (5 of 10) [7/20/2001 9:48:45 PM]
Programming Ruby: The Pragmatic Programmer's Guide
def threeTimes yield yield yield end threeTimes { puts "Hello" } produces: Hello Hello Hello The block (the code between the braces) is associated with the call to the method threeTimes. Within this method, yield is called three times in a row. Each time, it invokes the code in the block, and a cheery greeting is printed. What makes blocks interesting, however, is that you can pass parameters to them and receive values back from them. For example, we could write a simple function that returns members of the Fibonacci series up to a certain value.[The basic Fibonacci series is a sequence of integers, starting with two 1's, in which each subsequent term is the sum of the two preceding terms. The series is sometimes used in sorting algorithms and in analyzing natural phenomena.] def fibUpTo(max) i1, i2 = 1, 1 # parallel assignment while i1 aTime Returns the last access time for the named file. File.atime("testfile")
basename
» Sun Mar 04 23:25:54 CST 2001
File.basename( fileName [, aSuffix] ) -> aNewString Returns the last component of the filename given in fileName, which must be formed using forward slashes (``/'') regardless of the separator used on the local file system. If aSuffix is given and present at the end of fileName, it is removed. » "ruby.rb" File.basename("/home/gumby/work/ruby.rb") File.basename("/home/gumby/work/ruby.rb", ".rb") » "ruby"
chmod
File.chmod( aModeInt [, fileName]+ ) -> anInteger Changes permission bits on the named file(s) to the bit pattern represented by aModeInt. Actual effects are operating system dependent (see the beginning of this section). On Unix systems, see chmod(2) for details. Returns the number of files processed. File.chmod(0644, "testfile", "out")
http://www.rubycentral.com/book/ref_c_file.html (2 of 8) [7/20/2001 9:48:50 PM]
» 2
Programming Ruby: The Pragmatic Programmer's Guide
chown
File.chown( anOwnerInt, aGroupInt [, fileName]+ ) -> anInteger Changes the owner and group of the named file(s) to the given numeric owner and group id's. Only a process with superuser privileges may change the owner of a file. The current owner of a file may change the file's group to any group to which the owner belongs. A nil or -1 owner or group id is ignored. Returns the number of files processed. File.chown(nil, 100, "testfile")
ctime
File.ctime( fileName ) -> aTime Returns the change time for the named file (the time at which directory information about the file was changed, not the file itself). File.ctime("testfile")
delete
» Sun Mar 04 23:28:25 CST 2001
File.delete( [fileName]+ ) -> aFixnum Deletes the named file(s). Returns the number of files processed. See also Dir::rmdir. » nil » 1
File.new("testrm", "w+").close File.delete("testrm")
dirname
File.dirname( fileName ) -> fileName Returns all components of the filename given in fileName except the last one. The filename must be formed using forward slashes (``/'') regardless of the separator used on the local file system. File.dirname("/home/gumby/work/ruby.rb") » "/home/gumby/work"
expand_path
File.expand_path( fileName [, aDirString] ) -> fileName Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless aDirString is given, in which case it will be used as the starting point. The given pathname may start with a ``~'', which expands to the process owner's home directory (the environment variable HOME must be set correctly). ``~user'' expands to the named user's home directory. » "/home/oracle/bin" File.expand_path("~oracle/bin") File.expand_path("../../bin", "/tmp/x") » "/bin"
ftype
File.ftype( fileName ) -> fileType Identifies the type of the named file; the return string is one of ``file'', ``directory'', ``characterSpecial'', ``blockSpecial'', ``fifo'', ``link'', or ``socket''. File.ftype("testfile") File.ftype("/dev/tty") File.ftype("/tmp/.X11-unix/X0")
http://www.rubycentral.com/book/ref_c_file.html (3 of 8) [7/20/2001 9:48:50 PM]
» "file" » "characterSpecial" » "socket"
Programming Ruby: The Pragmatic Programmer's Guide
join
File.join( [aString]+ ) -> fileName Returns a new string formed by joining the strings using File::SEPARATOR (see Table 22.2 on page 308). » "usr/mail/gumby"
File.join("usr", "mail", "gumby")
link
File.link( anOldName, aNewName ) -> 0 Creates a new name for an existing file using a hard link. Will not overwrite aNewName if it already exists (raising a subclass of SystemCallError). Not available on all platforms. File.link("testfile", ".testfile") IO.readlines(".testfile")[0]
lstat
» 0 » "This is line one\n"
File.lstat( fileName ) -> aStat Same as IO#stat, but does not follow the last symbolic link. Instead, reports on the link itself. File.symlink("testfile", "link2test") File.stat("testfile").size File.lstat("link2test").size File.stat("link2test").size
mtime
» » » »
0 66 8 66
File.mtime( fileName ) -> aTime Returns the modification time for the named file. File.mtime("testfile")
new
» Tue Feb 27 00:11:18 CST 2001
File.new( fileName, aModeString="r" ) -> file File.new( fileName [, aModeNum [aPermNum]] ) -> file Opens the file named by fileName according to aModeString (default is ``r'') and returns a new File object. See Table 22.5 on page 331 for a description of aModeString. The file mode may optionally be specified as a Fixnum by or-ing together the flags described in Table 22.3 on page 309. Optional permission bits may be given in aPermNum. These mode and permission bits are platform dependent; on Unix systems, see open(2) for details. f = File.new("testfile", "r") f = File.new("newfile", "w+") f = File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)
open
File.open( fileName, aModeString="r" ) -> file File.open( fileName [, aModeNum [aPermNum]] ) -> file File.open( fileName, aModeString="r" ) {| file | block } -> nil File.open( fileName [, aModeNum [aPermNum]] ) {| file | block } -> nil
http://www.rubycentral.com/book/ref_c_file.html (4 of 8) [7/20/2001 9:48:50 PM]
Programming Ruby: The Pragmatic Programmer's Guide
With no associated block, open is a synonym for File::new. If the optional code block is given, it will be passed file as an argument, and the file will automatically be closed when the block terminates. In this instance, File::open returns nil.
readlink
File.readlink( fileName ) -> fileName Returns the given symbolic link as a string. Not available on all platforms. File.symlink("testfile", "link2test") File.readlink("link2test")
rename
» 0 » "testfile"
File.rename( anOldName, aNewName ) -> 0 Renames the given file to the new name. Raises a SystemCallError if the file cannot be renamed. » 0
File.rename("afile", "afile.bak")
size
File.size( fileName ) -> anInteger Returns the size of the file in bytes. File.size("testfile")
split
»
66
File.split( fileName ) -> anArray Splits the given string into a directory and a file component and returns them in a two-element array. See also File::dirname and File::basename. File.split("/home/gumby/.profile") » ["/home/gumby", ".profile"]
stat
File.stat( fileName ) -> aStat Returns a File::Stat object for the named file (see File::Stat, page 313). File.stat("testfile").mtime » Tue Feb 27 00:11:18 CST 2001
symlink
File.symlink( anOldName, aNewName ) -> 0 or nil Creates a symbolic link called aNewName for the existing file anOldName. Returns nil on all platforms that do not support symbolic links. File.symlink("testfile", "link2test")
truncate
File.truncate( fileName, anInteger ) -> 0
http://www.rubycentral.com/book/ref_c_file.html (5 of 8) [7/20/2001 9:48:50 PM]
» 0
Programming Ruby: The Pragmatic Programmer's Guide
Truncates the file fileName to be at most anInteger bytes long. Not available on all platforms. f = File.new("out", "w") f.write("1234567890") f.close File.truncate("out", 5) File.size("out")
umask
» » » »
10 nil 0 5
File.umask( [anInteger] ) -> anInteger Returns the current umask value for this process. If the optional argument is given, set the umask to that value and return the previous value. Umask values are subtracted from the default permissions; so a umask of 0222 would make a file read-only for everyone. See also the discussion of permissions on page 305. » »
File.umask(0006) File.umask
unlink
18 6
File.unlink( [fileName]+ ) -> anInteger Synonym for File::delete. See also Dir::rmdir.
utime
File.utime( anAccessTime, aModTime [, fileName]+ ) -> aFixnum Changes the access and modification times on a number of files. The times must be instances of class Time or integers representing the number of seconds since epoch. Returns the number of files processed. Not available on all platforms. File.utime(0, 0, "testfile") File.mtime("testfile") File.utime(0, Time.now, "testfile") File.mtime("testfile")
» » » »
1 Wed Dec 31 18:00:00 CST 1969 1 Sun Mar 04 23:28:25 CST 2001
Lock-mode constants LOCK_EX Exclusive lock. Only one process may hold an exclusive lock for a given file at a time. LOCK_NB Don't block when locking. May be combined with other lock options using logical or. LOCK_SH Shared lock. Multiple processes may each hold a shared lock for a given file at the same time. LOCK_UN Unlock.
instance methods atime
file.atime -> aTime
http://www.rubycentral.com/book/ref_c_file.html (6 of 8) [7/20/2001 9:48:50 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns the last access time for file, or epoch if file has not been accessed. File.new("testfile").atime
chmod
» Wed Dec 31 18:00:00 CST 1969
file.chmod( aModeInt ) -> 0 Changes permission bits on file to the bit pattern represented by aModeInt. Actual effects are platform dependent; on Unix systems, see chmod(2) for details. See the discussion of permissions on page 305. f = File.new("out", "w"); f.chmod(0644)
chown
»
0
file.chown( anOwnerInt, aGroupInt ) -> 0 Changes the owner and group of file to the given numeric owner and group id's. Only a process with superuser privileges may change the owner of a file. The current owner of a file may change the file's group to any group to which the owner belongs. A nil or -1 owner or group id is ignored. File.new("testfile").chown(502, 1000)
ctime
file.ctime -> aTime Returns the change time for file (that is, the time directory information about the file was changed, not the file itself). File.new("testfile").ctime
flock
» Sun Mar 04 23:28:25 CST 2001
file.flock ( aLockingConstant ) -> 0 or false Locks or unlocks a file according to aLockingConstant (a logical or of the values in Table 22.4 on page 312). Returns false if File::LOCK_NB is specified and the operation would otherwise have blocked. Not available on all platforms. File.new("testfile").flock(File::LOCK_UN)
lstat
» 0
file.lstat -> aStat Same as IO#stat, but does not follow the last symbolic link. Instead, reports on the link itself. File.symlink("testfile", "link2test") File.stat("testfile").size f = File.new("link2test") f.lstat.size f.stat.size
http://www.rubycentral.com/book/ref_c_file.html (7 of 8) [7/20/2001 9:48:50 PM]
» 0 » 66 » 8 » 66
Programming Ruby: The Pragmatic Programmer's Guide
mtime
file.mtime -> aTime
Returns the modification time for file. File.new("testfile").mtime
path
» Sun Mar 04 23:28:25 CST 2001
file.path -> fileName Returns the pathname used to create file as a string. » "testfile"
File.new("testfile").path
truncate
file.truncate( anInteger ) -> 0 Truncates file to at most anInteger bytes. The file must be opened for writing. Not available on all platforms. f = File.new("out", "w") f.syswrite("1234567890") f.truncate(5) f.close() File.size("out")
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Standard Types So far we've been having fun implementing pieces of our jukebox code, but we've been negligent. We've looked at arrays, hashes, and procs, but we haven't really covered the other basic types in Ruby: numbers, strings, ranges, and regular expressions. Let's spend a few pages on these basic building blocks now.
Numbers Ruby supports integers and floating point numbers. Integers can be any length (up to a maximum determined by the amount of free memory on your system). Integers within a certain range (normally -230 to 230-1 or -262 to 262-1) are held internally in binary form, and are objects of class Fixnum. Integers outside this range are stored in objects of class Bignum (currently implemented as a variable-length set of short integers). This process is transparent, and Ruby automatically manages the conversion back and forth. num = 8 7.times do print num.type, " ", num, "\n" num *= num end produces: Fixnum Fixnum Fixnum Fixnum Bignum Bignum Bignum
8 64 4096 16777216 281474976710656 79228162514264337593543950336 6277101735386680763835789423207666416102355444464034512896
You write integers using an optional leading sign, an optional base indicator (0 for octal, 0x for hex, or
http://www.rubycentral.com/book/tut_stdtypes.html (1 of 19) [7/20/2001 9:48:52 PM]
Programming Ruby: The Pragmatic Programmer's Guide
0b for binary), followed by a string of digits in the appropriate base. Underscore characters are ignored in the digit string. 123456 123_456 -543 123_456_789_123_345_789 0xaabb 0377 -0b101_010
# # # # # # #
Fixnum Fixnum (underscore ignored) Negative Fixnum Bignum Hexadecimal Octal Binary (negated)
You can also get the integer value corresponding to an ASCII character or escape sequence by preceding it with a question mark. Control and meta combinations can also be generated using ?\C-x, ?\M-x, and ?\M-\C-x. The control version of a value is the same as ``value & 0x9f''. The meta version of a value is ``value | 0x80''. Finally, the sequence ?\C-? generates an ASCII delete, 0177. ?a ?\n ?\C-a ?\M-a ?\M-\C-a ?\C-?
# # # # # #
character code code for a newline (0x0a) control a = ?A & 0x9f = 0x01 meta sets bit 7 meta and control a delete character
A numeric literal with a decimal point and/or an exponent is turned into a Float object, corresponding to the native architecture's double data type. You must follow the decimal point with a digit, as 1.e3 tries to invoke the method e3 in class Fixnum. All numbers are objects, and respond to a variety of messages (listed in full starting on pages 294, 318, 319, 328, and 354). So, unlike (say) C++, you find the absolute value of a number by writing aNumber.abs, not abs(aNumber). Integers also support several useful iterators. We've seen one already---7.times in the code example on page 49. Others include upto and downto, for iterating up and down between two integers, and step, which is more like a traditional for loop. 3.times 1.upto(5) 99.downto(95) 50.step(80, 5)
{ { { {
print "X " } |i| print i, " " } |i| print i, " " } |i| print i, " " }
produces: X X X 1 2 3 4 5 99 98 97 96 95 50 55 60 65 70 75 80 Finally, a warning for Perl users. Strings that contain numbers are not automatically converted into numbers when used in expressions. This tends to bite most often when reading numbers from a file. The following code (probably) doesn't do what was intended.
http://www.rubycentral.com/book/tut_stdtypes.html (2 of 19) [7/20/2001 9:48:52 PM]
Programming Ruby: The Pragmatic Programmer's Guide
DATA.each do |line| vals = line.split # split line, storing tokens in val print vals[0] + vals[1], " " end Feed it a file containing 3 4 5 6 7 8 and you'll get the output ``34 56 78.'' What happened? The problem is that the input was read as strings, not numbers. The plus operator concatenates strings, so that's what we see in the output. To fix this, use the String#to_i method to convert the string to an integer. DATA.each do |line| vals = line.split print vals[0].to_i + vals[1].to_i, " " end produces: 7 11 15
Strings Ruby strings are simply sequences of 8-bit bytes. They normally hold printable characters, but that is not a requirement; a string can also hold binary data. Strings are objects of class String. Strings are often created using string literals---sequences of characters between delimiters. Because binary data is otherwise difficult to represent within program source, you can place various escape sequences in a string literal. Each is replaced with the corresponding binary value as the program is compiled. The type of string delimiter determines the degree of substitution performed. Within single-quoted strings, two consecutive backslashes are replaced by a single backslash, and a backslash followed by a single quote becomes a single quote. 'escape using "\\"' 'That\'s right'
» escape using "\" » That's right
Double-quoted strings support a boatload more escape sequences. The most common is probably ``\n'', the newline character. Table 18.2 on page 205 gives the complete list. In addition, you can substitute the value of any Ruby expression into a string using the sequence #{ expr }. If the expression is just a global variable, a class variable, or an instance variable, you can omit the braces. "Seconds/day: #{24*60*60}"
» Seconds/day: 86400
http://www.rubycentral.com/book/tut_stdtypes.html (3 of 19) [7/20/2001 9:48:52 PM]
Programming Ruby: The Pragmatic Programmer's Guide
"#{'Ho! '*3}Merry Christmas" » Ho! Ho! Ho! Merry Christmas » This is line 3 "This is line #$." There are three more ways to construct string literals: %q, %Q, and ``here documents.'' %q and %Q start delimited single- and double-quoted strings. %q/general single-quoted string/ » general single-quoted string %Q!general double-quoted string! » general double-quoted string » Seconds/day: 86400 %Q{Seconds/day: #{24*60*60}} The character following the ``q'' or ``Q'' is the delimiter. If it is an opening bracket, brace, parenthesis, or less-than sign, the string is read until the matching close symbol is found. Otherwise the string is read until the next occurrence of the same delimiter. Finally, you can construct a string using a here document. aString = costs $12. » It costs $2.
Character class abbreviations Sequence \d \D \s \S \w \W
As [ ... ] [0-9] [^0-9] [\s\t\r\n\f] [^\s\t\r\n\f] [A-Za-z0-9_] [^A-Za-z0-9_]
Meaning Digit character Nondigit Whitespace character Nonwhitespace character Word character Nonword character
Finally, a period (``.'') appearing outside brackets represents any character except a newline (and in multiline mode it matches a newline, too). a = 'It costs $12.' showRE(a, /c.s/) showRE(a, /./) showRE(a, /\./)
» It ts $12. » t costs $12. » It costs $12
http://www.rubycentral.com/book/tut_stdtypes.html (13 of 19) [7/20/2001 9:48:52 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Repetition When we specified the pattern that split the song list line, /\s*\|\s*/, we said we wanted to match a vertical bar surrounded by an arbitrary amount of whitespace. We now know that the \s sequences match a single whitespace character, so it seems likely that the asterisks somehow mean ``an arbitrary amount.'' In fact, the asterisk is one of a number of modifiers that allow you to match multiple occurrences of a pattern. If r stands for the immediately preceding regular expression within a pattern, then: r* r+ r? r{m,n} r{m,}
matches zero or more occurrences of r. matches one or more occurrences of r. matches zero or one occurrence of r. matches at least ``m'' and at most ``n'' occurrences of r. matches at least ``m'' occurrences of r.
These repetition constructs have a high precedence---they bind only to the immediately preceding regular expression in the pattern. /ab+/ matches an ``a'' followed by one or more ``b''s, not a sequence of ``ab''s. You have to be careful with the * construct too---the pattern /a*/ will match any string; every string has zero or more ``a''s. These patterns are called greedy, because by default they will match as much of the string as they can. You can alter this behavior, and have them match the minimum, by adding a question mark suffix. a = "The moon is made of cheese" » moon is made of cheese showRE(a, /\w+/) » The>cheese showRE(a, /\s.*\s/) » The>is made of cheese showRE(a, /\s.*?\s/) showRE(a, /[aeiou]{2,99}/) » The mn is made of cheese » The >n is made of cheese showRE(a, /mo?o/)
Alternation We know that the vertical bar is special, because our line splitting pattern had to escape it with a backslash. That's because an unescaped vertical bar ``|'' matches either the regular expression that precedes it or the regular expression that follows it. a = "red ball blue sky" showRE(a, /d|e/) showRE(a, /al|lu/) showRE(a, /red ball|angry sky/)
» r>d ball blue sky » red bl blue sky » blue sky
There's a trap for the unwary here, as ``|'' has a very low precedence. The last example above matches ``red ball'' or ``angry sky'', not ``red ball sky'' or ``red angry sky''. To match ``red ball sky'' or ``red angry sky'', you'd need to override the default precedence using grouping.
http://www.rubycentral.com/book/tut_stdtypes.html (14 of 19) [7/20/2001 9:48:52 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Grouping You can use parentheses to group terms within a regular expression. Everything within the group is treated as a single regular expression. showRE('banana', /an*/) showRE('banana', /(an)*/) showRE('banana', /(an)+/) a = 'red ball blue sky' » showRE(a, /blue|red/) » showRE(a, /(blue|red) \w+/) » showRE(a, /(red|blue) \w+/) » showRE(a, /red|blue \w+/) showRE(a, /red (ball|angry) sky/) a = 'the red angry sky' showRE(a, /red (ball|angry) sky/)
» bana » banana » ba ball ball » no match
blue blue blue blue
sky sky sky sky
» the
Parentheses are also used to collect the results of pattern matching. Ruby counts opening parentheses, and for each stores the result of the partial match between it and the corresponding closing parenthesis. You can use this partial match both within the remainder of the pattern and in your Ruby program. Within the pattern, the sequence \1 refers to the match of the first group, \2 the second group, and so on. Outside the pattern, the special variables $1, $2, and so on, serve the same purpose. "12:50am" =~ /(\d\d):(\d\d)(..)/ "Hour is #$1, minute #$2" "12:50am" =~ /((\d\d):(\d\d))(..)/ "Time is #$1" "Hour is #$2, minute #$3" "AM/PM is #$4"
» » » » » »
0 "Hour is 12, minute 50" 0 "Time is 12:50" "Hour is 12, minute 50" "AM/PM is am"
The ability to use part of the current match later in that match allows you to look for various forms of repetition. # match duplicated letter showRE('He said "Hello"', /(\w)\1/) # match duplicated substrings showRE('Mississippi', /(\w+)\1/)
» He said "Heo" » Mippi
You can also use back references to match delimiters. showRE('He said "Hello"', /(["']).*?\1/) » He said showRE("He said 'Hello'", /(["']).*?\1/) » He said
http://www.rubycentral.com/book/tut_stdtypes.html (15 of 19) [7/20/2001 9:48:52 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Pattern-Based Substitution Sometimes finding a pattern in a string is good enough. If a friend challenges you to find a word that contains the letters a, b, c, d, and e in order, you could search a word list with the pattern /a.*b.*c.*d.*e/ and find ``absconded'' and ``ambuscade.'' That has to be worth something. However, there are times when you need to change things based on a pattern match. Let's go back to our song list file. Whoever created it entered all the artists' names in lowercase. When we display them on our jukebox's screen, they'd look better in mixed case. How can we change the first character of each word to uppercase? The methods String#sub and String#gsub look for a portion of a string matching their first argument and replace it with their second argument. String#sub performs one replacement, while String#gsub replaces every occurrence of the match. Both routines return a new copy of the String containing the substitutions. Mutator versions String#sub! and String#gsub! modify the original string. a = "the quick brown fox" a.sub(/[aeiou]/, '*') a.gsub(/[aeiou]/, '*') a.sub(/\s\S+/, '') a.gsub(/\s\S+/, '')
» » » »
"th* quick brown fox" "th* q**ck br*wn f*x" "the brown fox" "the"
The second argument to both functions can be either a String or a block. If a block is used, the block's value is substituted into the String. a = "the quick brown fox" a.sub(/^./) { $&.upcase } a.gsub(/[aeiou]/) { $&.upcase }
» "The quick brown fox" » "thE qUIck brOwn fOx"
So, this looks like the answer to converting our artists' names. The pattern that matches the first character of a word is \b\w---look for a word boundary followed by a word character. Combine this with gsub and we can hack the artists' names. def mixedCase(aName) aName.gsub(/\b\w/) { $&.upcase } end mixedCase("fats waller") mixedCase("louis armstrong") mixedCase("strength in numbers")
» "Fats Waller" » "Louis Armstrong" » "Strength In Numbers"
Backslash Sequences in the Substitution Earlier we noted that the sequences \1, \2, and so on are available in the pattern, standing for the nth group matched so far. The same sequences are available in the second argument of sub and gsub.
http://www.rubycentral.com/book/tut_stdtypes.html (16 of 19) [7/20/2001 9:48:52 PM]
Programming Ruby: The Pragmatic Programmer's Guide
"fred:smith".sub(/(\w+):(\w+)/, '\2, \1') "nercpyitno".gsub(/(.)(.)/, '\2\1')
» "smith, fred" » "encryption"
There are additional backslash sequences that work in substitution strings: \& (last match), \+ (last matched group), \` (string prior to match), \' (string after match), and \\ (a literal backslash). It gets confusing if you want to include a literal backslash in a substitution. The obvious thing is to write str.gsub(/\\/, '\\\\') Clearly, this code is trying to replace each backslash in str with two. The programmer doubled up the backslashes in the replacement text, knowing that they'd be converted to ``\\'' in syntax analysis. However, when the substitution occurs, the regular expression engine performs another pass through the string, converting ``\\'' to ``\'', so the net effect is to replace each single backslash with another single backslash. You need to write gsub(/\\/, '\\\\\\\\')! » "a\b\c" » "a\\b\\c"
str = 'a\b\c' str.gsub(/\\/, '\\\\\\\\')
However, using the fact that \& is replaced by the matched string, you could also write » "a\b\c" » "a\\b\\c"
str = 'a\b\c' str.gsub(/\\/, '\&\&')
If you use the block form of gsub, the string for substitution is analyzed only once (during the syntax pass) and the result is what you intended. » "a\b\c" » "a\\b\\c"
str = 'a\b\c' str.gsub(/\\/) { '\\\\' }
Finally, as an example of the wonderful expressiveness of combining regular expressions with code blocks, consider the following code fragment from the CGI library module, written by Wakou Aoyama. The code takes a string containing HTML escape sequences and converts it into normal ASCII. Because it was written for a Japanese audience, it uses the ``n'' modifier on the regular expressions, which turns off wide-character processing. It also illustrates Ruby's case expression, which we discuss starting on page 83. def unescapeHTML(string) str = string.dup str.gsub!(/&(.*?);/n) { match = $1.dup case match when /\Aamp\z/ni when /\Aquot\z/ni when /\Agt\z/ni when /\Alt\z/ni when /\A#(\d+)\z/n when /\A#x([0-9a-f]+)\z/ni end
then then then then then then
'&' '"' '>' ' anArray Returns arg.to_a. Array(1..5)
Float
» [1, 2, 3, 4, 5]
Float( arg ) -> aFloat Returns arg converted to a float. Numeric types are converted directly, nil is converted to 0.0, and the rest are converted using arg.to_f. Float(1) Float(nil) Float("123.456")
Integer
» » »
1.0 0.0 123.456
Integer( arg ) -> anInteger Converts arg to a Fixnum or Bignum. Numeric types are converted directly (with floating point numbers being truncated). If arg is a String, leading radix indicators (0, 0b, and 0x) are honored. This behavior is different from that of String#to_i. Integer(123.999) Integer("0x1a") Integer(Time.new)
String
String( arg ) -> aString
http://www.rubycentral.com/book/ref_m_kernel.html (1 of 16) [7/20/2001 9:48:55 PM]
» 123 » 26 » 983770240
Programming Ruby: The Pragmatic Programmer's Guide
Converts arg to a String by calling its to_s method. » » »
String(self) String(self.type) String(123456)
` (backquote)
"main" "Object" "123456"
`cmd` -> aString Returns the standard output of running cmd in a subshell. The built-in syntax %x{...} described on page 75 uses this method. `date` `ls testdir`.split[1]
abort
» "Sun Mar 4 23:30:40 CST 2001\n" » "main.rb"
abort Terminate execution immediately, effectively by calling Kernel.exit(1).
at_exit
at_exit { block } -> aProc Converts block to a Proc object (and therefore binds it at the point of call) and registers it for execution when the program exits. If multiple handlers are registered, they are executed in reverse order of registration. def do_at_exit(str1) at_exit { print str1 } end at_exit { puts "cruel world" } do_at_exit("goodbye ") exit produces: goodbye cruel world
autoload
autoload( aModule, aFile ) -> nil Registers aFile to be loaded (using Kernel::require) the first time that aModule (which may be a String or a symbol) is accessed. autoload :MyModule, "/usr/local/lib/modules/my_module.rb"
binding
binding -> aBinding Returns a Binding object, describing the variable and method bindings at the point of call. This object can be used when calling eval to execute the evaluated command in this environment. Also see the description of Binding beginning on page 295. def getBinding(param) return binding end b = getBinding("hello") eval "param", b
block_given?
block_given? -> true or false
http://www.rubycentral.com/book/ref_m_kernel.html (2 of 16) [7/20/2001 9:48:55 PM]
»
"hello"
Programming Ruby: The Pragmatic Programmer's Guide
Returns true if yield would execute a block in the current context. def try if block_given? yield else "no block" end end try try { "hello" } try do "hello" end
callcc
» »
"no block" "hello"
callcc {| cont | block } -> anObject Generates a Continuation object, which it passes to the associated block. Performing a cont.call will cause the callcc to return (as will falling through the end of the block). The value returned by the callcc is the value of the block, or the value passed to cont.call. See Continuation on page 298 for more details. Also see Kernel::throw for an alternative mechanism for unwinding a call stack.
caller
caller( [anInteger] ) -> anArray Returns the current execution stack---an array containing strings in the form ``file:line'' or ``file:line: in `method'''. The optional anInteger parameter determines the number of initial stack entries to omit from the result. def a(skip) caller(skip) end def b(skip) a(skip) end def c(skip) b(skip) end c(0) » ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10"] c(1) » ["prog:5:in `b'", "prog:8:in `c'", "prog:11"] c(2) » ["prog:8:in `c'", "prog:12"] c(3) » ["prog:13"]
catch
catch( symbol ) {| | block } -> anObject
http://www.rubycentral.com/book/ref_m_kernel.html (3 of 16) [7/20/2001 9:48:55 PM]
Programming Ruby: The Pragmatic Programmer's Guide
catch executes its block. If a throw is executed, Ruby searches up its stack for a catch block with a tag corresponding to the throw's symbol. If found, that block is terminated, and catch returns the value given to throw. If throw is not called, the block terminates normally, and the value of catch is the value of the last expression evaluated. catch expressions may be nested, and the throw call need not be in lexical scope. def routine(n) puts n throw :done if n $_ or aString Equivalent to $_ = $_.chomp(aString). See String#chomp on page 372. $_ = "now\n" chomp $_ chomp "ow" $_ chomp "xxx" $_
chomp!
» » » » » »
"now" "now" "n" "n" "n" "n"
chomp!( [aString] ) -> $_ or nil Equivalent to $_.chomp!(aString). See String#chomp! $_ = "now\n" chomp! $_ chomp! "x" $_
chop
» » » »
"now" "now" nil "now"
chop -> aString Equivalent to ($_.dup).chop!, except nil is never returned. See String#chop! on page 372. a = "now\r\n" $_ = a chop $_ chop chop chop chop a
» » » » » » »
"now" "now" "no" "n" "" "" "now\r\n"
http://www.rubycentral.com/book/ref_m_kernel.html (4 of 16) [7/20/2001 9:48:55 PM]
Programming Ruby: The Pragmatic Programmer's Guide
chop!
chop! -> $_ or nil Equivalent to $_.chop!. a = "now\r\n" $_ = a chop! chop! chop! chop! chop! $_ a
eval
» » » » » » »
"now" "no" "n" "" nil "" ""
eval( aString [, aBinding [file [line]]]) -> anObject Evaluates the Ruby expression(s) in aString. If aBinding is given, the evaluation is performed in its context. The binding may be a Binding object or a Proc object. If the optional file and line parameters are present, they will be used when reporting syntax errors. def getBinding(str) return binding end str = "hello" eval "str + ' Fred'" eval "str + ' Fred'", getBinding("bye")
exec
» "hello Fred" » "bye Fred"
exec( command [, args]) Replaces the current process by running the given external command. If exec is given a single argument, that argument is taken as a line that is subject to shell expansion before being executed. If multiple arguments are given, the second and subsequent arguments are passed as parameters to command with no shell expansion. If the first argument is a two-element array, the first element is the command to be executed, and the second argument is used as the argv[0] value, which may show up in process listings. In MSDOS environments, the command is executed in a subshell; otherwise, one of the exec(2) system calls is used, so the running command may inherit some of the environment of the original program (including open file descriptors).
exit
exec "echo *" # never get here
# echoes list of files in current directory
exec "echo", "*" # never get here
# echoes an asterisk
exit( anInteger=0 )
http://www.rubycentral.com/book/ref_m_kernel.html (5 of 16) [7/20/2001 9:48:55 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Initiates the termination of the Ruby script by raising the SystemExit exception. This exception may be caught. The optional parameter is used to return a status code to the invoking environment. begin exit puts "never get here" rescue SystemExit puts "rescued a SystemExit exception" end puts "after begin block" produces: rescued a SystemExit exception after begin block Just prior to termination, Ruby executes any at_exit functions and runs any object finalizers (see ObjectSpace beginning on page 434). at_exit { puts "at_exit function" } ObjectSpace.define_finalizer(self, proc { puts "in finalizer" }) exit produces: at_exit function 0x4019ac90 n finals=>0
exit!
exit!( anInteger=-1 ) Similar to Kernel::exit, but exception handling, at_exit functions, and finalizers are bypassed.
fail
fail fail( aString ) fail( anException [, aString [anArray]] ) Synonym for Kernel::raise.
fork
fork [{ block } ] -> aFixnum or nil
Creates a subshell. If a block is specified, that block is run in the subshell, and the subshell terminates with a status of zero. Otherwise, the fork call returns twice, once in the parent, returning the process id of the child, and once in the child, returning nil. The child process can exit using Kernel::exit! to avoid running any at_exit functions. The parent process should use Process::wait to collect the termination statuses of its children; otherwise, the operating system may accumulate zombie processes. fork do 3.times {|i| puts "Child: #{i}" } end 3.times {|i| puts "Parent: #{i}" } Process.wait produces:
http://www.rubycentral.com/book/ref_m_kernel.html (6 of 16) [7/20/2001 9:48:55 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Parent: 0 Child: 0 Child: 1 Parent: 1 Parent: 2 Child: 2
format
format( aString [, anObject]* ) -> aString Synonym for Kernel::sprintf.
gets
gets( aString=$/ ) -> aString or nil Returns (and assigns to $_) the next line from the list of files in ARGV (or $*), or from standard input if no files are present on the command line. Returns nil at end of file. The optional argument specifies the record separator. The separator is included with the contents of each record. A separator of nil reads the entire contents, and a zero-length separator reads the input one paragraph at a time, where paragraphs are divided by two consecutive newlines. If multiple filenames are present in ARGV, gets(nil) will read the contents one file at a time. ARGV anArray Returns an array of the names of global variables. global_variables.grep /std/ » ["$stderr", "$stdout", "$stdin"]
gsub
gsub( pattern, replacement ) -> aString gsub( pattern ) {| | block } -> aString Equivalent to $_.gsub..., except that $_ receives the modified result. $_ = "quick brown fox" gsub /[aeiou]/, '*' $_
gsub!
» "q**ck br*wn f*x" » "q**ck br*wn f*x"
gsub!( pattern, replacement ) -> aString or nil gsub!( pattern ) {| | block } -> aString or nil Equivalent to Kernel::gsub, except nil is returned if $_ is not modified. $_ = "quick brown fox" gsub! /cat/, '*' $_
» nil » "quick brown fox"
http://www.rubycentral.com/book/ref_m_kernel.html (7 of 16) [7/20/2001 9:48:55 PM]
Programming Ruby: The Pragmatic Programmer's Guide
iterator?
iterator? -> true or false
Synonym for Kernel::block_given?. The iterator? method will be removed in Ruby 1.8.
lambda
lambda {| | block } -> aProc Synonym for Kernel::proc.
load
load( aFileName, wrap=false ) -> true Loads and executes the Ruby program in the file aFileName. If the filename does not resolve to an absolute path, the file is searched for in the library directories listed in $:. If the optional wrap parameter is true, the loaded script will be executed under an anonymous module, protecting the calling program's global namespace. Any local variables in the loaded file will not be propagated to the loading environment.
local_variables
local_variables -> anArray Returns the names of the current local variables. fred = 1 for i in 1..10 # ... end local_variables
loop
» ["fred", "i"]
loop {| | block } Repeatedly executes the block. loop { print "Input: " break if !gets or $_ =~ /^qQ/ # ... }
open
open( aString [, aMode [perm]] ) -> anIO or nil open( aString [, aMode [perm]] ) {| anIO | block } -> nil
Creates an IO object connected to the given stream, file, or subprocess. If aString does not start with a pipe character (``|''), treat it as the name of a file to open using the specified mode defaulting to ``r'' (see the table of valid modes on page 331). If a file is being created, its initial permissions may be set using the integer third parameter. If a block is specified, it will be invoked with the File object as a parameter, and the file will be automatically closed when the block terminates. The call always returns nil in this case. If aString starts with a pipe character, a subprocess is created, connected to the caller by a pair of pipes. The returned IO object may be used to write to the standard input and read from the standard output of this subprocess. If the command following the ``|'' is a single minus sign, Ruby forks, and this subprocess is connected to the parent. In the subprocess, the open call returns nil. If the command is not ``-'', the subprocess runs the command. If a block is associated with an open("|-") call, that block will be run twice---once in the parent and once in the child. The block parameter will be an IO object in the parent and nil in the child. The parent's IO object will be connected to the
http://www.rubycentral.com/book/ref_m_kernel.html (8 of 16) [7/20/2001 9:48:56 PM]
Programming Ruby: The Pragmatic Programmer's Guide
child's $stdin and $stdout. The subprocess will be terminated at the end of the block. open("testfile") do |f| print f.gets end produces: This is line one Open a subprocess and read its output: cmd = open("|date") print cmd.gets cmd.close produces: Sun Mar
4 23:30:41 CST 2001
Open a subprocess running the same Ruby program: f = open("|-", "w+") if f == nil puts "in Child" exit else puts "Got: #{f.gets}" end produces: Got: in Child Open a subprocess using a block to receive the I/O object: open("|-") do |f| if f == nil puts "in Child" else puts "Got: #{f.gets}" end end produces: Got: in Child
p
p( [anObject]+ ) -> nil For each object, directly writes anObject.inspect followed by the current output record separator to the program's standard output. p bypasses the Ruby I/O libraries. p self produces: main
http://www.rubycentral.com/book/ref_m_kernel.html (9 of 16) [7/20/2001 9:48:56 PM]
Programming Ruby: The Pragmatic Programmer's Guide
print
print( [anObject]* ) -> nil Prints each object in turn to $defout. If the output field separator ($,) is not nil, its contents will appear between each field. If the output record separator ($\) is not nil, it will be appended to the output. If no arguments are given, prints $_. Objects that aren't strings will be converted by calling their to_s method. print "cat", [1,2,3], 99, "\n" $, = ", " $\ = "\n" print "cat", [1,2,3], 99 produces: cat12399 cat, 1, 2, 3, 99
printf
printf( anIO, aString [, anObject]* ) -> nil printf( aString [, anObject]* ) -> nil Equivalent to: anIO.write sprintf(aString, anObject ...) or $defout.write sprintf(aString, anObject ...)
proc
proc { block } -> aProc Creates a new procedure object from the given block. Equivalent to Proc::new. aProc = proc { "hello" } aProc.call
putc
»
"hello"
putc( anInteger ) -> anInteger Equivalent to $defout.putc(anInteger).
puts
puts( [args]* ) -> nil Equivalent to $defout.puts(args).
raise
raise raise( aString ) raise( anException [, aString [anArray]] ) With no arguments, raises the exception in $! or raises a RuntimeError if $! is nil. With a single String argument, raises a RuntimeError with the string as a message. Otherwise, the first parameter should be the name of an Exception class (or an object that returns an Exception when sent exception). The optional second parameter sets the message associated with the exception, and the third parameter is an array of callback information. Exceptions are caught by the rescue clause of begin...end blocks. raise "Failed to create socket" raise ArgumentError, "No parameters", caller
rand
rand( max=0 ) -> aNumber
http://www.rubycentral.com/book/ref_m_kernel.html (10 of 16) [7/20/2001 9:48:56 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Converts max to an integer using max1 = max.to_i.abs. If the result is zero, returns a pseudorandom floating point number greater than or equal to 0.0 and less than 1.0. Otherwise, returns a pseudorandom integer greater than or equal to zero and less than max1. Kernel::srand may be used to ensure repeatable sequences of random numbers between different runs of the program. srand 1234 [ rand, rand ] [ rand(10), rand(1000) ] srand 1234 [ rand, rand ]
readline
» » » » »
0 [0.7408769294, 0.2145348572] [3, 323] 1234 [0.7408769294, 0.2145348572]
readline( [aString=$/] ) -> aString Equivalent to Kernel::gets, except readline raises EOFError at end of file.
readlines
readlines( [aString=$/] ) -> anArray Returns an array containing the lines returned by calling Kernel.gets(aString) until the end of file.
require
require( aString ) -> true or false Ruby tries to load the library named aString, returning true if successful. If the filename does not resolve to an absolute path, it will be searched for in the directories listed in $:. If the file has the extension ``.rb'', it is loaded as a source file; if the extension is ``.so'', ``.o'', or ``.dll'',[Or whatever the default shared library extension is on the current platform.] Ruby loads the shared library as a Ruby extension. Otherwise, Ruby tries adding ``.rb'', ``.so'', and so on to the name. The name of the loaded feature is added to the array in $". A feature will not be loaded if it already appears in $". require returns true if the feature was successfully loaded. require "my-library.rb" require "db-driver"
scan
scan( pattern ) -> anArray scan( pattern ) {| | block } -> $_ Equivalent to calling $_.scan. See String#scan on page 378.
select
select( readArray [, writeArray [errorArray [timeout]]] ) -> anArray or nil Performs a low-level select call, which waits for data to become available from input/output devices. The first three parameters are arrays of IO objects or nil. The last is a timeout in seconds, which should be an Integer or a Float. The call waits for data to become available for any of the IO objects in readArray, for buffers to have cleared sufficiently to enable writing to any of the devices in writeArray, or for an error to occur on the devices in errorArray. If one or more of these conditions are met, the call returns a three-element array containing arrays of the IO objects that were ready. Otherwise, if there is no change in status for timeout seconds, the call returns nil. If all parameters are nil, the current thread sleeps forever. select( [$stdin], nil, nil, 1.5 ) » [[#], [], []]
set_trace_func
set_trace_func( aProc ) -> aProc set_trace_func( nil ) -> nil
http://www.rubycentral.com/book/ref_m_kernel.html (11 of 16) [7/20/2001 9:48:56 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Establishes aProc as the handler for tracing, or disables tracing if the parameter is nil. aProc takes up to six parameters: an event name, a filename, a line number, an object id, a binding, and the name of a class. aProc is invoked whenever an event occurs. Events are: c-call (call a C-language routine), c-return (return from a C-language routine), call (call a Ruby method), class (start a class or module definition), end (finish a class or module definition), line (execute code on a new line), raise (raise an exception), and return (return from a Ruby method). Tracing is disabled within the context of aProc. See the example starting on page 271 for more information.
singleton_method_added
singleton_method_added( aFixnum ) -> nil Invoked with a symbol id whenever a singleton method is added to a module or a class. The default implementation in Kernel ignores this, but subclasses may override the method to provide specialized functionality. class Test def Test.singleton_method_added(id) puts "Added #{id.id2name} to Test" end def a() end def Test.b() end end def Test.c() end produces: Added singleton_method_added to Test Added b to Test Added c to Test
sleep
sleep( [aNumeric] ) -> aFixnum Suspends the current thread for aNumber seconds (which may be a Float with fractional seconds). Returns the actual number of seconds slept (rounded), which may be less than that asked for if the thread was interrupted by a SIGALRM, or if another thread calls Thread#run. An argument of zero causes sleep to sleep forever. Time.new sleep 1.2 Time.new sleep 1.9 Time.new
split
» » » » »
Sun Mar 04 23:30:41 CST 2001 2 Sun Mar 04 23:30:43 CST 2001 2 Sun Mar 04 23:30:45 CST 2001
split( [pattern [limit]] ) -> anArray Equivalent to $_.split(pattern, limit). See String#split on page 379.
sprintf
sprintf( aFormatString [, arguments]* ) -> aString
http://www.rubycentral.com/book/ref_m_kernel.html (12 of 16) [7/20/2001 9:48:56 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns the string resulting from applying aFormatString to any additional arguments. Within the format string, any characters other than format sequences are copied to the result. A format sequence consists of a percent sign, followed by optional flags, width, and precision indicators, then terminated with a field type character. The field type controls how the corresponding sprintf argument is to be interpreted, while the flags modify that interpretation. The flag characters are shown in Table 23.1 on page 428, and the field type characters are listed in Table 23.2. The field width is an optional integer, followed optionally by a period and a precision. The width specifies the minimum number of characters that will be written to the result for this field. For numeric fields, the precision controls the number of decimal places displayed. For string fields, the precision determines the maximum number of characters to be copied from the string. (Thus, the format sequence %10.10s will always contribute exactly ten characters to the result.) sprintf flag characters Flag Applies to Meaning (space) bdeEfgGioxXu Leave a space at the start of positive numbers. beEfgGoxX Use an alternative format. For the conversions `o', `x', `X', # and `b', prefix the result with ``0'', ``0x'', ``0X'', and ``0b'', respectively. For `e', `E', `f', `g', and 'G', force a decimal point to be added, even if no digits follow. For `g' and 'G', do not remove trailing zeros. bdeEfgGioxXu Add a leading plus sign to positive numbers. + all Left-justify the result of this conversion. Pad with zeros, not spaces. 0 (zero) all all Use the next argument as the field width. If negative, * left-justify the result. If the asterisk is followed by a number and a dollar sign, use the indicated argument as the width. sprintf field types Field b c d E e
f G g i o s u X x
Conversion Convert argument as a binary number. Argument is the numeric code for a single character. Convert argument as a decimal number. Equivalent to `e', but uses an uppercase E to indicate the exponent. Convert floating point argument into exponential notation with one digit before the decimal point. The precision determines the number of fractional digits (defaulting to six). Convert floating point argument as [ -]ddd.ddd, where the precision determines the number of digits after the decimal point. Equivalent to `g', but use an uppercase `E' in exponent form. Convert a floating point number using exponential form if the exponent is less than -4 or greater than or equal to the precision, or in d.dddd form otherwise. Identical to `d'. Convert argument as an octal number. Argument is a string to be substituted. If the format sequence contains a precision, at most that many characters will be copied. Treat argument as an unsigned decimal number. Convert argument as a hexadecimal number using uppercase letters. Convert argument as a hexadecimal number.
sprintf("%d %04x", 123, 123) sprintf("%08b '%4s'", 123, 123) sprintf("%*2$s %d", "hello", 10)
http://www.rubycentral.com/book/ref_m_kernel.html (13 of 16) [7/20/2001 9:48:56 PM]
» "123 007b" » "01111011 ' 123'" » " hello 10"
Programming Ruby: The Pragmatic Programmer's Guide
» "hello -10" sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23) » "+1.23: 1.23:1.23" sprintf("%*2$s %d", "hello", -10)
srand
srand( [aNumber] ) -> oldSeed Seeds the pseudorandom number generator to the value of aNumber.to_i.abs. If aNumber is omitted or zero, seeds the generator using a combination of the time, the process id, and a sequence number. (This is also the behavior if Kernel::rand is called without previously calling srand, but without the sequence.) By setting the seed to a known value, scripts can be made deterministic during testing. The previous seed value is returned. Also see Kernel::rand on page 425.
sub
sub( pattern, replacement ) -> $_ sub( pattern ) { block } -> $_ Equivalent to $_.sub(args), except that $_ will be updated if substitution occurs.
sub!
sub!( pattern, replacement ) -> $_ or nil sub!( pattern ) { block } -> $_ or nil Equivalent to $_.sub!(args).
syscall
syscall( aFixnum [, args]* ) -> anInteger Calls the operating system function identified by aFixnum, passing in the arguments, which must be either String objects, or Integer objects that ultimately fit within a native long. Up to nine parameters may be passed (14 on the Atari-ST). The function identified by Fixnum is system dependent. On some Unix systems, the numbers may be obtained from a header file called syscall.h. syscall 4, 1, "hello\n", 6
# '4' is write(2) on our box
produces: hello
system
system( aCmd [, args]* ) -> true or false Executes aCmd in a subshell, returning true if the command was found and ran successfully, false otherwise. A detailed error code is available in $?. The arguments are processed in the same way as for Kernel::exec on page 419. system("echo *") system("echo", "*") produces: config.h main.rb *
test
test(aCmd, file1 [, file2] ) -> anObject
http://www.rubycentral.com/book/ref_m_kernel.html (14 of 16) [7/20/2001 9:48:56 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Uses the integer aCmd to perform various tests on file1 (Table 23.3 on page 430) or on file1 and file2 (Table 23.4). File tests with a single argument Integer ?A ?b ?c ?C ?d ?e ?f ?g ?G ?k ?l ?M ?o ?O ?p ?r ?R ?s ?S ?u ?w ?W ?x ?X ?z
Description Last access time for file1 True if file1 is a block device True if file1 is a character device Last change time for file1 True if file1 exists and is a directory True if file1 exists True if file1 exists and is a regular file True if file1 has the setgid bit set (false under NT) True if file1 exists and has a group ownership equal to the caller's group True if file1 exists and has the sticky bit set True if file1 exists and is a symbolic link Last modification time for file1 True if file1 exists and is owned by the caller's effective uid True if file1 exists and is owned by the caller's real uid True if file1 exists and is a fifo True if file is readable by the effective uid/gid of the caller True if file is readable by the real uid/gid of the caller If file1 has nonzero size, return the size, otherwise return nil True if file1 exists and is a socket True if file1 has the setuid bit set True if file1 exists and is writable by the effective uid/gid True if file1 exists and is writable by the real uid/gid True if file1 exists and is executable by the effective uid/gid True if file1 exists and is executable by the real uid/gid True if file1 exists and has a zero length
Returns Time true or false true or false Time true or false true or false true or false true or false true or false true or false true or false Time true or false true or false true or false true or false true or false Integer or nil true or false true or false true or false true or false true or false true or false true or false
File tests with two arguments Integer ??= ?< ?>
throw
Description True if file1 is a hard link to file2 True if the modification times of file1 and file2 are equal True if the modification time of file1 is prior to that of file2 True if the modification time of file1 is after that of file2
throw( aSymbol [, anObject] ) Transfers control to the end of the active catch block waiting for aSymbol. Raises NameError if there is no catch block for the symbol. The optional second parameter supplies a return value for the catch block, which otherwise defaults to nil. For examples, see Kernel::catch on page 417.
trace_var
trace_var( aSymbol, aCmd ) -> nil trace_var( aSymbol ) {| val | block } -> nil
http://www.rubycentral.com/book/ref_m_kernel.html (15 of 16) [7/20/2001 9:48:56 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Controls tracing of assignments to global variables. The parameter aSymbol identifies the variable (as either a string name or a symbol identifier). cmd (which may be a string or a Proc object) or block is executed whenever the variable is assigned. The block or Proc object receives the variable's new value as a parameter. Also see Kernel::untrace_var. trace_var :$_, proc {|v| puts "$_ is now '#{v}'" } $_ = "hello" $_ = ' there' produces: $_ is now 'hello' $_ is now ' there'
trap
trap( signal, cmd ) -> anObject trap( signal ) {| | block } -> anObject Specifies the handling of signals. The first parameter is a signal name (a string such as ``SIGALRM'', ``SIGUSR1'', and so on) or a signal number. The characters ``SIG'' may be omitted from the signal name. The command or block specifies code to be run when the signal is raised. If the command is the string ``IGNORE'' or ``SIG_IGN'', the signal will be ignored. If the command is ``DEFAULT'' or ``SIG_DFL'', the operating system's default handler will be invoked. If the command is ``EXIT'', the script will be terminated by the signal. Otherwise, the given command or block will be run. The special signal name ``EXIT'' or signal number zero will be invoked just prior to program termination. trap returns the previous handler for the given signal. trap 0, proc { puts "Terminating: #{$$}" } trap("CLD") { puts "Child died" } fork && Process.wait produces: Terminating: 16422 Child died Terminating: 16421
untrace_var
untrace_var( aSymbol [, aCmd] ) -> anArray or nil Removes tracing for the specified command on the given global variable and returns nil. If no command is specified, removes all tracing for that variable and returns an array containing the commands actually removed.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: new [ ] arity call Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables. def genTimes(factor) return Proc.new {|n| n*factor } end times3 = genTimes(3) times5 = genTimes(5) times3.call(12) times5.call(5) times3.call(times5.call(4))
class methods new
Proc.new [{| | block } ] -> aProc
http://www.rubycentral.com/book/ref_c_proc.html (1 of 3) [7/20/2001 9:48:57 PM]
» 36 » 25 » 60
Programming Ruby: The Pragmatic Programmer's Guide
Creates a new Proc object, bound to the current context. It may be called without a block only within a method with an attached block, in which case that block is converted to the Proc object. def procFrom Proc.new end aProc = procFrom { "hello" } aProc.call
»
"hello"
instance methods []
prc[ [params]* ] -> anObject Synonym for Proc.call.
arity
prc.arity -> anInteger Returns the number of arguments required by the block. If the block takes no arguments, returns -1. If it takes one argument, returns -2. Otherwise, returns a positive argument count unless the last argument is prefixed with *, in which case the argument count is negated. The number of required arguments is anInteger for positive values, and (anInteger+1).abs otherwise. Proc.new Proc.new Proc.new Proc.new Proc.new Proc.new
call
{||}.arity {|a|}.arity {|a,b|}.arity {|a,b,c|}.arity {|*a|}.arity {|a,*b|}.arity
» » » » » »
0 -1 2 3 -1 -2
prc.call( [params]* ) -> anObject Invokes the block, setting the block's parameters to the values in params using the same rules as used by parallel assignment. Returns the value of the last expression evaluated in the block. aProc = Proc.new {|a, *b| b.collect {|i| i*a }} » [9, 18, 27] aProc.call(9, 1, 2, 3) » [9, 18, 27] aProc[9, 1, 2, 3]
http://www.rubycentral.com/book/ref_c_proc.html (2 of 3) [7/20/2001 9:48:57 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
More About Methods Other languages have functions, procedures, methods, or routines, but in Ruby there is only the method---a chunk of expressions that return a value. So far in this book, we've been defining and using methods without much thought. Now it's time to get into the details.
Defining a Method As we've seen throughout this book, a method is defined using the keyword def. Method names should begin with a lowercase letter.[You won't get an immediate error if you use an uppercase letter, but when Ruby sees you calling the method, it will first guess that it is a constant, not a method invocation, and as a result it may parse the call incorrectly.] Methods that act as queries are often named with a trailing ``?'', such as instance_of?. Methods that are ``dangerous,'' or modify the receiver, might be named with a trailing ``!''. For instance, String provides both a chop and a chop!. The first one returns a modified string; the second modifies the receiver in place. ``?'' and ``!'' are the only weird characters allowed as method name suffixes. Now that we've specified a name for our new method, we may need to declare some parameters. These are simply a list of local variable names in parentheses. Some sample method declarations are def myNewMethod(arg1, arg2, arg3) # 3 arguments # Code for the method would go here end def myOtherNewMethod # No arguments # Code for the method would go here end Ruby lets you specify default values for a method's arguments---values that will be used if the caller doesn't pass them explicitly. This is done using the assignment operator. def coolDude(arg1="Miles", arg2="Coltrane", arg3="Roach")
http://www.rubycentral.com/book/tut_methods.html (1 of 7) [7/20/2001 9:48:58 PM]
Programming Ruby: The Pragmatic Programmer's Guide
"#{arg1}, #{arg2}, #{arg3}." end coolDude coolDude("Bart") coolDude("Bart", "Elwood") coolDude("Bart", "Elwood", "Linus")
» » » »
"Miles, Coltrane, Roach." "Bart, Coltrane, Roach." "Bart, Elwood, Roach." "Bart, Elwood, Linus."
The body of a method contains normal Ruby expressions, except that you may not define an instance method, class, or module within a method. The return value of a method is the value of the last expression executed, or the result of an explicit return expression.
Variable-Length Argument Lists But what if you want to pass in a variable number of arguments, or want to capture multiple arguments into a single parameter? Placing an asterisk before the name of the parameter after the ``normal'' parameters does just that. def varargs(arg1, *rest) "Got #{arg1} and #{rest.join(', ')}" end varargs("one") varargs("one", "two") varargs "one", "two", "three"
» "Got one and " » "Got one and two" » "Got one and two, three"
In this example, the first argument is assigned to the first method parameter as usual. However, the next parameter is prefixed with an asterisk, so all the remaining arguments are bundled into a new Array, which is then assigned to that parameter.
Methods and Blocks As we discussed in the section on blocks and iterators beginning on page 40, when a method is called, it may be associated with a block. Normally, you simply call the block from within the method using yield. def takeBlock(p1) if block_given? yield(p1) else p1 end end » "no block" takeBlock("no block") takeBlock("no block") { |s| s.sub(/no /, '') } » "block" http://www.rubycentral.com/book/tut_methods.html (2 of 7) [7/20/2001 9:48:58 PM]
Programming Ruby: The Pragmatic Programmer's Guide
However, if the last parameter in a method definition is prefixed with an ampersand, any associated block is converted to a Proc object, and that object is assigned to the parameter. class TaxCalculator def initialize(name, &block) @name, @block = name, block end def getTax(amount) "#@name on #{amount} = #{ @block.call(amount) }" end end tc = TaxCalculator.new("Sales tax") { |amt| amt * 0.075 } tc.getTax(100) tc.getTax(250)
» "Sales tax on 100 = 7.5" » "Sales tax on 250 = 18.75"
Calling a Method You call a method by specifying a receiver, the name of the method, and optionally some parameters and an associated block. connection.downloadMP3("jitterbug") { |p| showProgress(p) } In this example, the object connection is the receiver, downloadMP3 is the name of the method, "jitterbug" is the parameter, and the stuff between the braces is the associated block. For class and module methods, the receiver will be the class or module name. File.size("testfile") Math.sin(Math::PI/4) If you omit the receiver, it defaults to self, the current object. self.id id self.type type
» » » »
537712200 537712200 Object Object
This defaulting mechanism is how Ruby implements private methods. Private methods may not be called with a receiver, so they must be methods available in the current object. The optional parameters follow the method name. If there is no ambiguity you can omit the parentheses around the argument list when calling a method.[Other Ruby documentation sometimes calls these method calls without parentheses ``commands.''] However, except in the simplest cases we don't recommend this---there are some subtle problems that can trip you up.[In particular, you must use http://www.rubycentral.com/book/tut_methods.html (3 of 7) [7/20/2001 9:48:58 PM]
Programming Ruby: The Pragmatic Programmer's Guide
parentheses on a method call that is itself a parameter to another method call (unless it is the last parameter).] Our rule is simple: if there's any doubt, use parentheses. a = obj.hash a = obj.hash()
# Same as # this.
obj.someMethod "Arg1", arg2, arg3 obj.someMethod("Arg1", arg2, arg3)
# Same thing as # with parentheses.
Expanding Arrays in Method Calls Earlier we saw that if you put an asterisk in front of a formal parameter in a method definition, multiple arguments in the call to the method will be bundled up into an array. Well, the same thing works in reverse. When you call a method, you can explode an array, so that each of its members is taken as a separate parameter. Do this by prefixing the array argument (which must follow all the regular arguments) with an asterisk. def five(a, b, c, d, e) "I was passed #{a} #{b} #{c} #{d} #{e}" end » "I was passed 1 2 3 4 5" five(1, 2, 3, 4, 5 ) five(1, 2, 3, *['a', 'b']) » "I was passed 1 2 3 a b" » "I was passed 10 11 12 13 14" five(*(10..14).to_a)
Making Blocks More Dynamic We've already seen how you can associate a block with a method call. listBones("aardvark") do |aBone| # ... end Normally, this is perfectly good enough---you associate a fixed block of code with a method, in the same way you'd have a chunk of code after an if or while statement. Sometimes, however, you'd like to be more flexible. For example, we may be teaching math skills.[Of course, Andy and Dave would have to learn math skills first. Conrad Schneiker reminded us that there are three kinds of people: those who can count and those who can't.] The student could ask for an n-plus table or an n-times table. If the student asked for a 2-times table, we'd output 2, 4, 6, 8, and so on. (This code does not check its inputs for errors.)
http://www.rubycentral.com/book/tut_methods.html (4 of 7) [7/20/2001 9:48:58 PM]
Programming Ruby: The Pragmatic Programmer's Guide
print "(t)imes or (p)lus: " times = gets print "number: " number = gets.to_i if times =~ /^t/ puts((1..10).collect { |n| n*number }.join(", ")) else puts((1..10).collect { |n| n+number }.join(", ")) end produces: (t)imes or (p)lus: t number: 2 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 This works, but it's ugly, with virtually identical code on each branch of the if statement. If would be nice if we could factor out the block that does the calculation. print "(t)imes or (p)lus: " times = gets print "number: " number = gets.to_i if times =~ /^t/ calc = proc { |n| n*number } else calc = proc { |n| n+number } end puts((1..10).collect(&calc).join(", ")) produces: (t)imes or (p)lus: t number: 2 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 If the last argument to a method is preceded by an ampersand, Ruby assumes that it is a Proc object. It removes it from the parameter list, converts the Proc object into a block, and associates it with the method. This technique can also be used to add some syntactic sugar to block usage. For example, you sometimes want to take an iterator and store each value it yields into an array. We'll reuse our Fibonacci number generator from page 42.
http://www.rubycentral.com/book/tut_methods.html (5 of 7) [7/20/2001 9:48:58 PM]
Programming Ruby: The Pragmatic Programmer's Guide
a = [] fibUpTo(20) { |val| a 270 } ) The first parameter is the search name, and the second is a hash literal containing search parameters. The use of a hash means that we can simulate keywords: look for songs with a genre of ``jazz'' and a duration less than 4 1/2 minutes. However, this approach is slightly clunky, and that set of braces could easily be mistaken for a block associated with the method. So, Ruby has a short cut. You can place key => value pairs in an argument list, as long as they follow any normal arguments and precede any array and block arguments. All these pairs will be collected into a single hash and passed as one argument to the method. No braces are needed. aList.createSearch("short jazz songs", 'genre' => "jazz", 'durationLessThan' => 270 ) http://www.rubycentral.com/book/tut_methods.html (6 of 7) [7/20/2001 9:48:58 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: new % * + aNewString Returns a new string object containing a copy of aString.
instance methods %
str % arg -> aString Format---Uses str as a format specification, and returns the result of applying it to arg. If the format specification contains more than one substitution, then arg must be an Array containing the values to be substituted. See Kernel::sprintf on page 427 for details of the format string. "%05d" % 123 "%-5s: %08x" % [ "ID", self.id ]
» "00123" » "ID :
http://www.rubycentral.com/book/ref_c_string.html (1 of 16) [7/20/2001 9:49:00 PM]
200cd648"
Programming Ruby: The Pragmatic Programmer's Guide
*
str * anInteger -> aString
Copy---Returns a new String containing anInteger copies of the receiver. »
"Ho! " * 3
+
"Ho! Ho! Ho! "
str + aString -> aNewString Concatenation---Returns a new String containing aString concatenated to str. » "Hello from main"
"Hello from " + self.to_s
aFixnum or nil
http://www.rubycentral.com/book/ref_c_string.html (2 of 16) [7/20/2001 9:49:01 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Match---If anObject is a Regexp or a String, uses it as a pattern to match against str. Returns the position the match starts, or nil if there is no match. Otherwise, invokes anObject.=~, passing str as an argument. The default =~ in Object returns false. » 7 » 7 » false
"cat o' 9 tails" =~ "\\d" "cat o' 9 tails" =~ /\d/ "cat o' 9 tails" =~ 9
[]
str[ aFixnum ] -> aFixnum or nil str[ aFixnum, aFixnum ] -> aString or nil str[ aRange ] -> aString or nil str[ aRegexp ] -> aString or nil str[ aString ] -> aString or nil Element Reference---If passed a single Fixnum, returns the code of the character at that position. If passed two Fixnum objects, returns a substring starting at the offset given by the first, and a length given by the second. If given a range, a substring containing characters at offsets given by the range is returned. In all three cases, if an offset is negative, it is counted from the end of str. Returns nil if the initial offset falls outside the string, the length is negative, or the beginning of the range is greater than the end. If a Regexp is supplied, the matching portion of str is returned. If a String is given, that string is returned if it occurs in str. In both cases, nil is returned if there is no match. a = "hello there" a[1] a[1,3] a[1..3] a[-3,2] a[-4..-2] a[-2..-4] a[/th[aeiou]/] a["lo"] a["bye"]
[ ]=
» » » » » » » » »
101 "ell" "ell" "er" "her" nil "the" "lo" nil
str[ aFixnum ] = aFixnum str[ aFixnum ] = aString str[ aFixnum, aFixnum ] = aString str[ aRange ] = aString str[ aRegexp ] = aString str[ aString ] = aString Element Assignment---Replaces some or all of the content of str. The portion of the string affected is determined using the same criteria as String#[]. If the replacement string is not the same length as the text it is replacing, the string will be adjusted accordingly. The forms that take a Fixnum will raise an IndexError if the value is out of range; the Range form will raise a RangeError, and the Regexp and String forms will silently ignore the assignment. a a a a a a a a a
= = = = = = = = =
"hello"; "hello"; "hello"; "hello"; "hello"; "hello"; "hello"; "hello"; "hello";
a[2] a[2, 4] a[-4, 2] a[2..4] a[-4..-2] a[/[el]+/] a["l"] a["ll"] a["bad"]
= = = = = = = = =
96; "xyz"; "xyz"; "xyz"; "xyz"; "xyz"; "xyz"; "xyz"; "xyz";
a a a a a a a a a
http://www.rubycentral.com/book/ref_c_string.html (3 of 16) [7/20/2001 9:49:01 PM]
» » » » » » » » »
"he`lo" "hexyz" "hxyzlo" "hexyz" "hxyzo" "hxyzo" "hexyzlo" "hexyzo" "hello"
Programming Ruby: The Pragmatic Programmer's Guide
a = "hello"; a[2, 0]
~
» "hexyzllo"
= "xyz"; a
~str -> aFixnum or nil Equivalent to $_ =~ str.
capitalize
str.capitalize -> aString Returns a copy of str with the first character converted to uppercase and the remainder to lowercase. » "Hello" » "Hello" » "123abc"
"hello".capitalize "HELLO".capitalize "123ABC".capitalize
capitalize!
str.capitalize! -> str or nil Modifies str by converting the first character to uppercase and the remainder to lowercase. Returns nil if no changes are made. a = "hello" a.capitalize! a a.capitalize!
center
» » »
"Hello" "Hello" nil
str.center( anInteger ) -> aString If anInteger is greater than the length of str, returns a new String of length anInteger with str centered between spaces; otherwise, returns str. "hello".center(4) "hello".center(20)
chomp
» "hello" » "
hello
"
str.chomp( aString=$/ ) -> aString Returns a new String with the given record separator removed from the end of str (if present). "hello".chomp "hello\n".chomp "hello \n there".chomp "hello".chomp("llo")
chomp!
» » » »
"hello" "hello" "hello \n there" "he"
str.chomp!( aString=$/ ) -> str or nil Modifies str in place as described for String#chomp, returning str, or nil if no modifications were made.
chop
str.chop -> aString
http://www.rubycentral.com/book/ref_c_string.html (4 of 16) [7/20/2001 9:49:01 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns a new String with the last character removed. If the string ends with \r\n, both characters are removed. Applying chop to an empty string returns an empty string. String#chomp is often a safer alternative, as it leaves the string unchanged if it doesn't end in a record separator. "string\r\n".chop "string\n\r".chop "string\n".chop "string".chop "x".chop.chop
chop!
» » » » »
"string" "string\n" "string" "strin" ""
str.chop! -> str or nil Processes str as for String#chop, returning str, or nil if str is the empty string. See also String#chomp!.
concat
str.concat( aFixnum ) -> str str.concat( anObject ) -> str Synonym for String# aString Applies a one-way cryptographic hash to str by invoking the standard library function crypt. The argument is the salt string, which should be two characters long, each character drawn from [a-zA-Z0-9./].
delete
str.delete( [aString]+ ) -> aString Returns a copy of str with all characters in the intersection of its arguments deleted. Uses the same rules for building the set of characters as String#count. "hello".delete "hello".delete "hello".delete "hello".delete
delete!
"l","lo" "lo" "aeiou", "^e" "ej-m"
» » » »
"heo" "he" "hell" "ho"
str.delete!( [aString]+ ) -> str or nil Performs a delete operation in place, returning str, or nil if str was not modified.
downcase
str.downcase -> aString
http://www.rubycentral.com/book/ref_c_string.html (5 of 16) [7/20/2001 9:49:01 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns a copy of str with all uppercase letters replaced with their lowercase counterparts. The operation is locale insensitive---only characters ``A'' to ``Z'' are affected. "hEllO".downcase
downcase!
»
"hello"
str.downcase! -> str or nil Downcases the contents of str, returning nil if no changes were made.
dump
str.dump -> aString Produces a version of str with all nonprinting characters replaced by \nnn notation and all special characters escaped.
each
str.each( aString=$/ ) {| substr | block } -> str Splits str using the supplied parameter as the record separator ($/ by default), passing each substring in turn to the supplied block. If a zero-length record separator is supplied, the string is split on \n characters, except that multiple successive newlines are appended together. print "Example one\n" "hello\nworld".each {|s| p s} print "Example two\n" "hello\nworld".each('l') {|s| p s} print "Example three\n" "hello\n\n\nworld".each('') {|s| p s} produces: Example one "hello\n" "world" Example two "hel" "l" "o\nworl" "d" Example three "hello\n\n\n" "world"
each_byte
str.each_byte {| aFixnum | block } -> str Passes each byte in str to the given block. "hello".each_byte {|c| print c, ' ' } produces: 104 101 108 108 111
each_line
str.each_line( aString=$/ ) {| substr | block } -> str Synonym for String#each.
http://www.rubycentral.com/book/ref_c_string.html (6 of 16) [7/20/2001 9:49:01 PM]
Programming Ruby: The Pragmatic Programmer's Guide
empty?
str.empty? -> true or false Returns true if str has a length of zero. "hello".empty? "".empty?
gsub
» »
false true
str.gsub( pattern, replacement ) -> aString str.gsub( pattern ) {| match | block } -> aString Returns a copy of str with all occurrences of pattern replaced with either replacement or the value of the block. If a string is used as the replacement, special variables from the match (such as $& and $1) cannot be substituted into it, as substitution into the string occurs before the pattern match starts. However, the sequences \1, \2, and so on may be used to interpolate successive groups in the match. These sequences are shown in Table 22.7 on page 376. Backslash sequences in substitution strings Sequence Text That Is Substituted \1, \2, ... \9 The value matched by the nth grouped subexpression The last match \& The part of the string before the match \` The part of the string after the match \' The highest-numbered group matched \+ In the block form, the current match is passed in as a parameter, and variables such as $1, $2, $`, $&, and $' will be set appropriately. The value returned by the block will be substituted for the match on each call. The result inherits any tainting in the original string or any supplied replacement string. » "h*ll*" "hello".gsub(/[aeiou]/, '*') » "h<e>ll" "hello".gsub(/([aeiou])/, '') "hello".gsub('.') {|s| s[0].to_s + ' '} » "104 101 108 108 111 "
gsub!
str.gsub!( pattern, replacement ) -> str or nil str.gsub!( pattern ) {| match | block } -> str or nil Performs the substitutions of String#gsub in place, returning str, or nil if no substitutions were performed.
hash
str.hash -> aFixnum Generates a Fixnum hash value for str. If $= is true, the hash will be case insensitive. $= = true hash = { 'cat' => 'Feline', 'dog' => 'canine' } » "Feline" hash['cat'] » "Feline" hash['cAt'] $= = false hash.rehash # re-calculate hash values » {"dog"=>"canine", "cat"=>"Feline"} » "Feline" hash['cat'] » nil hash['cAt']
hex
str.hex -> anInteger
http://www.rubycentral.com/book/ref_c_string.html (7 of 16) [7/20/2001 9:49:01 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Treats leading characters from str as a string of hexadecimal digits (with an optional sign and an optional 0x) and returns the corresponding number. Zero is returned on error. » » » »
"0x0a".hex "-1234".hex "0".hex "wombat".hex
include?
10 -4660 0 0
str.include? aString -> true or false str.include? aFixnum -> true or false Returns true if str contains the given string or character. » true » false » true
"hello".include? "lo" "hello".include? "ol" "hello".include? ?h
index
str.index( aString [, anOffset] ) -> aFixnum or nil str.index( aFixnum [, anOffset] ) -> aFixnum or nil str.index( aRegexp [, anOffset] ) -> aFixnum or nil Returns the index of the first occurrence of the given substring, character, or pattern in str. Returns nil if not found. If the second parameter is present, it specifies the position in the string to begin the search. » » » » »
"hello".index('e') "hello".index('lo') "hello".index('a') "hello".index(101) "hello".index(/[aeiou]/, -3)
intern
1 3 nil 1 4
str.intern -> aSymbol Returns the Symbol corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name on page 389. »
"Koala".intern
length
:Koala
str.length -> anInteger Returns the length of str.
ljust
str.ljust( anInteger ) -> aString If anInteger is greater than the length of str, returns a new String of length anInteger with str left justified and space padded; otherwise, returns str. "hello".ljust(4) "hello".ljust(20)
next
» "hello" » "hello
str.next -> aString Synonym for String#succ.
next!
str.next! -> str
http://www.rubycentral.com/book/ref_c_string.html (8 of 16) [7/20/2001 9:49:01 PM]
"
Programming Ruby: The Pragmatic Programmer's Guide
Synonym for String#succ!.
oct
str.oct -> anInteger Treats str as a string of octal digits (with an optional sign) and returns the corresponding number. Returns 0 if the conversion fails. » » » »
"123".oct "-377".oct "bad".oct "0377bad".oct
replace
83 -255 0 255
str.replace( aString ) -> str Replaces the contents and taintedness of str with the corresponding values in aString. » »
s = "hello" s.replace "world"
reverse
"hello" "world"
str.reverse -> aString Returns a new string with the characters from str in reverse order. » "desserts"
"stressed".reverse
reverse!
str.reverse! -> str Reverses str in place.
rindex
str.rindex( aString [, aFixnum] ) -> aFixnum or nil str.rindex( aFixnum [, aFixnum] ) -> aFixnum or nil str.rindex( aRegexp [, aFixnum] ) -> aFixnum or nil Returns the index of the last occurrence of the given substring, character, or pattern in str. Returns nil if not found. If the second parameter is present, it specifies the position in the string to end the search---characters beyond this point will not be considered. "hello".rindex('e') "hello".rindex('l') "hello".rindex('a') "hello".rindex(101) "hello".rindex(/[aeiou]/, -2)
rjust
» » » » »
1 3 nil 1 1
str.rjust( anInteger ) -> aString If anInteger is greater than the length of str, returns a new String of length anInteger with str right justified and space padded; otherwise, returns str. "hello".rjust(4) "hello".rjust(20)
scan
» "hello" » "
str.scan( pattern ) -> anArray str.scan( pattern ) {| match, ...| block } -> str
http://www.rubycentral.com/book/ref_c_string.html (9 of 16) [7/20/2001 9:49:01 PM]
hello"
Programming Ruby: The Pragmatic Programmer's Guide
Both forms iterate through str, matching the pattern (which may be a Regexp or a String). For each match, a result is generated and either added to the result array or passed to the block. If the pattern contains no groups, each individual result consists of the matched string, $&. If the pattern contains groups, each individual result is itself an array containing one entry per group. a = "cruel world" a.scan(/\w+/) a.scan(/.../) a.scan(/(...)/) a.scan(/(..)(..)/)
» » » »
["cruel", "world"] ["cru", "el ", "wor"] [["cru"], ["el "], ["wor"]] [["cr", "ue"], ["l ", "wo"]]
And the block form: a.scan(/\w+/) {|w| print " " } print "\n" a.scan(/(.)(.)/) {|a,b| print b, a } print "\n" produces: > rceu lowlr
size
str.size -> anInteger Synonym for String#length.
slice
str.slice( aFixnum ) -> aFixnum or nil str.slice( aFixnum, aFixnum ) -> aString or nil str.slice( aRange ) -> aString or nil str.slice( aRegexp ) -> aString or nil str.slice( aString ) -> aString or nil Synonym for String#[ ]. a = "hello there" a.slice(1) a.slice(1,3) a.slice(1..3) a.slice(-3,2) a.slice(-4..-2) a.slice(-2..-4) a.slice(/th[aeiou]/) a.slice("lo") a.slice("bye")
slice!
str.slice!( aFixnum ) -> aFixnum or nil str.slice!( aFixnum, aFixnum ) -> aString or nil str.slice!( aRange ) -> aString or nil str.slice!( aRegexp ) -> aString or nil str.slice!( aString ) -> aString or nil
http://www.rubycentral.com/book/ref_c_string.html (10 of 16) [7/20/2001 9:49:01 PM]
» » » » »
101 "ell" "ell" "er" "her" » nil » "the" » "lo" » nil
Programming Ruby: The Pragmatic Programmer's Guide
Deletes the specified portion from str, and returns the portion deleted. The forms that take a Fixnum will raise an IndexError if the value is out of range; the Range form will raise a RangeError, and the Regexp and String forms will silently ignore the assignment. string = "this is a string" string.slice!(2) string.slice!(3..6) string.slice!(/s.*t/) string.slice!("r") string
split
» » » » »
105 " is " "sa st" "r" "thing"
str.split( pattern=$;, [limit] ) -> anArray Divides str into substrings based on a delimiter, returning an array of these substrings. If pattern is a String, then its contents are used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, with leading whitespace and runs of contiguous whitespace characters ignored. If pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters. If pattern is omitted, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ` ' were specified. If the limit parameter is omitted, trailing null fields are supressed. If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not supressed. " now's the time".split " now's the time".split(' ') " now's the time".split(/ /) "1, 2.34,56, 7".split(/,\s*/) "hello".split(//) "hello".split(//, 3) "hi mom".split(/\s*/) "mellow yellow".split("ello") "1,2,,3,4,,".split(',') "1,2,,3,4,,".split(',', 4) "1,2,,3,4,,".split(',', -4)
squeeze
» » » » » » » » » » »
["now's", "the", "time"] ["now's", "the", "time"] ["", "now's", "", "the", "time"] ["1", "2.34", "56", "7"] ["h", "e", "l", "l", "o"] ["h", "e", "llo"] ["h", "i", "m", "o", "m"] ["m", "w y", "w"] ["1", "2", "", "3", "4"] ["1", "2", "", "3,4,,"] ["1", "2", "", "3", "4", "", ""]
str.squeeze( [aString]* ) -> aNewString Builds a set of characters from the aString parameter(s) using the procedure described for String#count on page 373. Returns a new string where runs of the same character that occur in this set are replaced by a single character. If no arguments are given, all runs of identical characters are replaced by a single character. "yellow moon".squeeze " now is the".squeeze(" ") "putters shoot balls".squeeze("m-z")
squeeze!
» "yelow mon" » " now is the" » "puters shot balls"
str.squeeze!( [aString]* ) -> str or nil Squeezes str in place, returning either str, or nil if no changes were made.
strip
str.strip -> aString
http://www.rubycentral.com/book/ref_c_string.html (11 of 16) [7/20/2001 9:49:01 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns a copy of str with leading and trailing whitespace removed. » "hello" » "goodbye"
" hello ".strip "\tgoodbye\r\n".strip
strip!
str.strip! -> str or nil Removes leading and trailing whitespace from str. Returns nil if str was not altered.
sub
str.sub( pattern, replacement ) -> aString str.sub( pattern ) {| match | block } -> aString Returns a copy of str with the first occurrence of pattern replaced with either replacement or the value of the block. If the string form of the method is used, special variables such as $& will not be useful, as substitution into the string occurs before the pattern match starts. However, the sequences \1, \2, listed in Table 22.7 on page 376 may be used. In the block form, the current match is passed in as a parameter, and variables such as $1, $2, $`, $&, and $' will be set appropriately. The value returned by the block will be substituted for the match on each call. "hello".sub(/[aeiou]/, '*') "hello".sub(/([aeiou])/, '') "hello".sub('.') {|s| s[0].to_s + ' ' }
sub!
» "h*llo" » "h<e>llo" » "104 ello"
str.sub!( pattern, replacement ) -> str or nil str.sub!( pattern ) {| match | block } -> str or nil Performs the substitutions of String#sub in place, returning str, or nil if no substitutions were performed.
succ
str.succ -> aString Returns the successor to str. The successor is calculated by incrementing characters starting from the rightmost alphanumeric (or the rightmost character if there are no alphanumerics) in the string. Incrementing a digit always results in another digit, and incrementing a letter results in another letter of the same case. Incrementing nonalphanumerics uses the underlying character set's collating sequence. If the increment generates a ``carry,'' the character to the left of it is incremented. This process repeats until there is no carry, adding an additional character if necessary. "abcd".succ "THX1138".succ "".succ "1999zzz".succ "ZZZ9999".succ "***".succ
succ!
» » » » » »
"abce" "THX1139" "" "2000aaa" "AAAA0000" "**+"
str.succ! -> str Equivalent to String#succ, but modifies the receiver in place.
sum
str.sum( aFixnum=16 ) -> anInteger Returns a basic n-bit checksum of the characters in str, where n is the optional parameter, defaulting to 16. The result is simply the sum of the binary value of each character in str modulo 2n - 1. This is not a particularly good checksum.
http://www.rubycentral.com/book/ref_c_string.html (12 of 16) [7/20/2001 9:49:01 PM]
Programming Ruby: The Pragmatic Programmer's Guide
swapcase
str.swapcase -> aString
Returns a copy of str with uppercase alphabetic characters converted to lowercase and lowercase characters converted to uppercase. "Hello".swapcase "cYbEr_PuNk11".swapcase
swapcase!
» "hELLO" » "CyBeR_pUnK11"
str.swapcase! -> str or nil Equivalent to String#swapcase, but modifies the receiver in place, returning str, or nil if no changes were made.
to_f
str.to_f -> aFloat Returns the result of interpreting leading characters in str as a floating point number. Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0.0 is returned. The method never raises an exception. "123.45e1".to_f "45.67 degrees".to_f "thx1138".to_f
to_i
» 1234.5 » 45.67 » 0.0
str.to_i -> anInteger Returns the result of interpreting leading characters in str as a decimal integer. Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0 is returned. The method never raises an exception. "12345".to_i "99 red balloons".to_i "0x0a".to_i "hello".to_i
to_s
» » » »
12345 99 0 0
str.to_s -> str Returns the receiver.
to_str
str.to_str -> str Synonym for String#to_s. to_str is used by methods such as String#concat to convert their arguments to a string. Unlike to_s, which is supported by almost all classes, to_str is normally implemented only by those classes that act like strings. Of the built-in classes, only Exception and String implement to_str.
tr
str.tr( fromString, toString ) -> aString
http://www.rubycentral.com/book/ref_c_string.html (13 of 16) [7/20/2001 9:49:01 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns a copy of str with the characters in fromString replaced by the corresponding characters in toString. If toString is shorter than fromString, it is padded with its last character. Both strings may use the c1--c2 notation to denote ranges of characters, and fromString may start with a ^, which denotes all characters except those listed. "hello".tr('aeiou', '*') "hello".tr('^aeiou', '*') "hello".tr('el', 'ip') "hello".tr('a-y', 'b-z')
tr!
» » » »
"h*ll*" "*e**o" "hippo" "ifmmp"
str.tr!( fromString, toString ) -> str or nil Translates str in place, using the same rules as String#tr. Returns str, or nil if no changes were made.
tr_s
str.tr_s( fromString, toString ) -> aString Processes a copy of str as described under String#tr, then removes duplicate characters in regions that were affected by the translation. "hello".tr_s('l', 'r') "hello".tr_s('el', '*') "hello".tr_s('el', 'hx')
tr_s!
» "hero" » "h*o" » "hhxo"
str.tr_s!( fromString, toString ) -> str or nil Performs String#tr_s processing on str in place, returning str, or nil if no changes were made.
unpack
str.unpack( format ) -> anArray Decodes str (which may contain binary data) according to the format string, returning an array of each value extracted. The format string consists of a sequence of single-character directives, summarized in Table 22.8 on page 384. Each directive may be followed by a number, indicating the number of times to repeat with this directive. An asterisk (``*'') will use up all remaining elements. The directives sSiIlL may each be followed by an underscore (``_'') to use the underlying platform's native size for the specified type; otherwise, it uses a platform-independent consistent size. Spaces are ignored in the format string. See also Array#pack on page 290. Directives for String#unpack Format A a B b C c d E e f G g H h
Function String with trailing nulls and spaces removed. String. Extract bits from each character (msb first). Extract bits from each character (lsb first). Extract a character as an unsigned integer. Extract a character as an integer. Treat sizeof(double) characters as a native double. Treat sizeof(double) characters as a double in little-endian byte order. Treat sizeof(float) characters as a float in little-endian byte order. Treat sizeof(float) characters as a native float. Treat sizeof(double) characters as a double in network byte order. Treat sizeof(float) characters as a float in network byte order. Extract hex nibbles from each character (most significant first). Extract hex nibbles from each character (least significant first).
http://www.rubycentral.com/book/ref_c_string.html (14 of 16) [7/20/2001 9:49:01 PM]
Returns String String String String Fixnum Fixnum Float Float Float Float Float Float String String
Programming Ruby: The Pragmatic Programmer's Guide
I
Treat sizeof(int)1 successive characters as an unsigned native integer.
Integer
i
Treat sizeof(int)1 successive characters as a signed native integer.
Integer
L
Treat four1 successive characters as an unsigned native long integer.
Integer
l
Treat four1 successive characters as a signed native long integer. Extract a quoted-printable string. Extract a base64 encoded string. Treat four characters as an unsigned long in network byte order. Treat two characters as an unsigned short in network byte order. Treat sizeof(char *) characters as a pointer, and return len characters from the referenced location. Treat sizeof(char *) characters as a pointer to a null-terminated string.
Integer
S
Treat two1 successive characters as an unsigned short in native byte order.
Fixnum
s
Treat two1 successive characters as a signed short in native byte order. Extract UTF-8 characters as unsigned integers. Extract a UU-encoded string. Treat four characters as an unsigned long in little-endian byte order. Treat two characters as an unsigned short in little-endian byte order. Skip backward one character. Skip forward one character. String with trailing nulls removed. Skip to the offset given by the length argument.
Fixnum
M m N n P p
U u V v X x Z @ 1
String
Integer String Fixnum Fixnum ----String ---
May be modified by appending ``_'' to the directive.
"abc \0\0abc \0\0".unpack('A6Z6') "abc \0\0".unpack('a3a3') "aa".unpack('b8B8') "aaa".unpack('h2H2c') "\xfe\xff\xfe\xff".unpack('sS') "now=20is".unpack('M*') "whole".unpack('xax2aX2aX1aX2a')
upcase
String String Fixnum Fixnum String
» » » » » » »
["abc", "abc "] ["abc", " \000\000"] ["10000110", "01100001"] ["16", "61", 97] [-2, 65534] ["now is"] ["h", "e", "l", "l", "o"]
str.upcase -> aString Returns a copy of str with all lowercase letters replaced with their uppercase counterparts. The operation is locale insensitive---only characters ``a'' to ``z'' are affected. "hEllO".upcase
upcase!
»
"HELLO"
str.upcase! -> str or nil Upcases the contents of str, returning nil if no changes were made.
upto
str.upto( aString ) {| s | block } -> str
http://www.rubycentral.com/book/ref_c_string.html (15 of 16) [7/20/2001 9:49:01 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Iterates through successive values, starting at str and ending at aString inclusive, passing each value in turn to the block. The String#succ method is used to generate each value. "a8".upto("b6") {|s| print s, ' ' } for s in "a8".."b6" print s, ' ' end produces: a8 a9 b0 b1 b2 b3 b4 b5 b6 a8 a9 b0 b1 b2 b3 b4 b5 b6
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
class Regexp
Parent: Object Version: 1.6
Index: compile escape last_match new quote == === =~ ~ casefold? kcode match source A Regexp holds a regular expression, used to match a pattern against strings. Regexps are created using the /.../ and %r{...} literals, and by the Regexp::new constructor.
constants EXTENDED
Ignore spaces and newlines in regexp.
IGNORECASE
Matches are case insensitive.
MULTILINE
Newlines treated as any other character.
class methods compile
Regexp.compile( pattern [, options [lang]] ) -> aRegexp Synonym for Regexp.new.
escape
Regexp.escape( aString ) -> aNewString Escapes any characters that would have special meaning in a regular expression. For any string, Regexp.escape(str)=~str will be true. Regexp.escape('\\*?{}.')
http://www.rubycentral.com/book/ref_c_regexp.html (1 of 4) [7/20/2001 9:49:02 PM]
» \\\\\*\?\{\}\.
Programming Ruby: The Pragmatic Programmer's Guide
last_match
Regexp.last_match -> aMatchData
Returns the MatchData object generated by the last successful pattern match. Equivalent to reading the global variable $~. MatchData is described on page 340.
new
Regexp.new( pattern [, options [lang]] ) -> aRegexp Constructs a new regular expression from pattern, which can be either a String or a Regexp (in which case that regexp's options are not propagated). If options is a Fixnum, it should be one or more of the constants Regexp::EXTENDED, Regexp::IGNORECASE, and Regexp::POSIXLINE, or-ed together. Otherwise, if options is not nil, the regexp will be case insensitive. The lang parameter enables multibyte support for the regexp: `n', `N' = none, `e', `E' = EUC, `s', `S' = SJIS, `u', `U' = UTF-8. r1 = Regexp.new('^a-z+:\\s+\w+') r2 = Regexp.new(r1, true) r3 = Regexp.new(r2, Regexp::EXTENDED)
quote
» /^a-z+:\s+\w+/ » /^a-z+:\s+\w+/i » /^a-z+:\s+\w+/x
Regexp.quote( aString ) -> aNewString Synonym for Regexp.escape.
instance methods ==
rxp == aRegexp -> true or false Equality---Two regexps are equal if their patterns are identical, they have the same character set code, and their casefold? values are the same. /abc/ == /abc/x /abc/ == /abc/i /abc/u == /abc/n
===
rxp === aString -> true or false
http://www.rubycentral.com/book/ref_c_regexp.html (2 of 4) [7/20/2001 9:49:02 PM]
» » »
true false false
Programming Ruby: The Pragmatic Programmer's Guide
Case Equality---Synonym for Regexp#=~ used in case statements. a = "HELLO" case a when /^a-z*$/; print "Lower case\n" when /^A-Z*$/; print "Upper case\n" else; print "Mixed case\n" end produces: Upper case
=~
rxp =~ aString -> anInteger or nil Match---Matches rxp against aString, returning the offset of the start of the match or nil if the match failed. » nil » 5
/SIT/ =~ "insensitive" /SIT/i =~ "insensitive"
~
~ rxp -> anInteger or nil Match---Matches rxp against the contents of $_. Equivalent to rxp =~ $_. $_ = "input data" ~ /at/
casefold?
»
7
rxp.casefold? -> true or false Returns the value of the case-insensitive flag.
kcode
rxp.kcode -> aString Returns the character set code for the regexp.
match
rxp.match(aString) -> aMatchData or nil Returns a MatchData object (see page 340) describing the match, or nil if there was no match. This is equivalent to retrieving the value of the special variable $~ following a normal match. /(.)(.)(.)/.match("abc")[2]
http://www.rubycentral.com/book/ref_c_regexp.html (3 of 4) [7/20/2001 9:49:02 PM]
» "b"
Programming Ruby: The Pragmatic Programmer's Guide
source
rxp.source -> aString Returns the original string of the pattern. »
/ab+c/ix.source
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Expressions So far we've been fairly cavalier in our use of expressions in Ruby. After all, a=b+c is pretty standard stuff. You could write a whole heap of Ruby code without reading any of this chapter. But it wouldn't be as much fun ;-). One of the first differences with Ruby is that anything that can reasonably return a value does: just about everything is an expression. What does this mean in practice? Some obvious things include the ability to chain statements together. a = b = c = 0 [ 3, 1, 7, 0 ].sort.reverse
» 0 » [7, 3, 1, 0]
Perhaps less obvious, things that are normally statements in C or Java are expressions in Ruby. For example, the if and case statements both return the value of the last expression executed. songType = if song.mp3Type == MP3::Jazz if song.written < Date.new(1935, 1, 1) Song::TradJazz else Song::Jazz end else Song::Other end rating = case votesCast when 0...10 then Rating::SkipThisOne when 10...50 then Rating::CouldDoBetter else Rating::Rave end We'll talk more about if and case starting on page 81.
http://www.rubycentral.com/book/tut_expressions.html (1 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Operator Expressions Ruby has the basic set of operators (+, -, *, /, and so on) as well as a few surprises. A complete list of the operators, and their precedences, is given in Table 18.4 on page 221. In Ruby, many operators are actually method calls. When you write a*b+c you're actually asking the object referenced by a to execute the method ``*'', passing in the parameter b. You then ask the object that results from that calculation to execute ``+'', passing c as a parameter. This is exactly equivalent to writing (a.*(b)).+(c) Because everything is an object, and because you can redefine instance methods, you can always redefine basic arithmetic if you don't like the answers you're getting. class Fixnum alias oldPlus + def +(other) oldPlus(other).succ end end 1 + 2 a = 3 a += 4
»
4
»
8
More useful is the fact that classes that you write can participate in operator expressions just as if they were built-in objects. For example, we might want to be able to extract a number of seconds of music from the middle of a song. We could using the indexing operator ``[]'' to specify the music to be extracted. class Song def [](fromTime, toTime) result = Song.new(self.title + " [extract]", self.artist, toTime - fromTime) result.setStartTime(fromTime) result end end This code fragment extends class Song with the method ``[]'', which takes two parameters (a start time and an end time). It returns a new song, with the music clipped to the given interval. We could then play the introduction to a song with code such as:
http://www.rubycentral.com/book/tut_expressions.html (2 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
aSong[0, 0.15].play
Miscellaneous Expressions As well as the obvious operator expressions and method calls, and the (perhaps) less obvious statement expressions (such as if and case), Ruby has a few more things that you can use in expressions.
Command Expansion If you enclose a string in backquotes, or use the delimited form prefixed by %x, it will (by default) be executed as a command by your underlying operating system. The value of the expression is the standard output of that command. Newlines will not be stripped, so it is likely that the value you get back will have a trailing return or linefeed character. `date` `dir`.split[34] %x{echo "Hello there"}
» "Sun Mar 4 23:23:52 CST 2001\n" » "lib_pstore.txi" » "Hello there\n"
You can use expression expansion and all the usual escape sequences in the command string. for i in 0..3 status = `dbmanager status id=#{i}` # ... end The exit status of the command is available in the global variable $?.
Backquotes Are Soft In the description of the command output expression, we said that the string in backquotes would ``by default'' be executed as a command. In fact, the string is passed to the method called Kernel::` (a single backquote). If you want, you can override this. alias oldBackquote ` def `(cmd) result = oldBackquote(cmd) if $? != 0 raise "Command #{cmd} failed" end result end print `date` print `data`
http://www.rubycentral.com/book/tut_expressions.html (3 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
produces: Sun Mar 4 23:23:52 CST 2001 prog.rb:3: command not found: data prog.rb:5:in ``': Command data failed (RuntimeError) from prog.rb:10
Assignment Just about every example we've given so far in this book has featured assignment. Perhaps it's about time we said something about it. An assignment statement sets the variable or attribute on its left side (the lvalue) to refer to the value on the right (the rvalue). It then returns that value as the result of the assignment expression. This means that you can chain assignments and that you can perform assignments in some unexpected places. a = b = 1 + 2 + 3 » a » b a = (b = 1 + 2) + 3 » a » b File.open(name = gets.chomp)
6 6 6 3
There are two basic forms of assignment in Ruby. The first assigns an object reference to a variable or constant. This form of assignment is hard-wired into the language. instrument = "piano" MIDDLE_A = 440 The second form of assignment involves having an object attribute or element reference on the left-hand side. aSong.duration = 234 instrument["ano"] = "ccolo" These forms are special, because they are implemented by calling methods in the lvalues, which means you can override them. We've already seen how to define a writable object attribute. Simply define a method name ending in an equals sign. This method receives as its parameter the assignment's rvalue.
http://www.rubycentral.com/book/tut_expressions.html (4 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class Song def duration=(newDuration) @duration = newDuration end end There is no reason that these attribute setting methods must correspond with internal instance variables, or that there has to be an attribute reader for every attribute writer (or vice versa). class Amplifier def volume=(newVolume) self.leftChannel = self.rightChannel = newVolume end # ... end Sidebar: Using Accessors Within a Class Why did we write self.leftChannel in the example on page 76? Well, there's a hidden gotcha with writable attributes. Normally, methods within a class can invoke other methods in the same class and its superclasses in functional form (that is, with an implicit receiver of self). However, this doesn't work with attribute writers. Ruby sees the assignment and decides that the name on the left must be a local variable, not a method call to an attribute writer. class BrokenAmplifier attr_accessor :leftChannel, :rightChannel def volume=(vol) leftChannel = self.rightChannel = vol end end ba = BrokenAmplifier.new ba.leftChannel = ba.rightChannel = 99 ba.volume = 5 ba.leftChannel ba.rightChannel
» »
99 5
We forgot to put ``self.'' in front of the assignment to leftChannel, so Ruby stored the new value in a local variable of method volume=; the object's attribute never got updated. This can be a tricky bug to track down.
http://www.rubycentral.com/book/tut_expressions.html (5 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Parallel Assignment During your first week in a programming course (or the second semester if it was a party school), you may have had to write code to swap the values in two variables: int a = 1; int b = 2; int temp; temp = a; a = b; b = temp; You can do this much more cleanly in Ruby: a, b = b, a Ruby assignments are effectively performed in parallel, so the values assigned are not affected by the assignment itself. The values on the right-hand side are evaluated in the order in which they appear before any assignment is made to variables or attributes on the left. A somewhat contrived example illustrates this. The second line assigns to the variables a, b, and c the values of the expressions x, x+=1, and x+=1, respectively. x = 0 a, b, c
=
x, (x += 1), (x += 1)
» 0 » [0, 1, 2]
When an assignment has more than one lvalue, the assignment expression returns an array of the rvalues. If an assignment contains more lvalues than rvalues, the excess lvalues are set to nil. If a multiple assignment contains more rvalues than lvalues, the extra rvalues are ignored. As of Ruby 1.6.2, if an assignment has one lvalue and multiple rvalues, the rvalues are converted to an array and assigned to the lvalue. You can collapse and expand arrays using Ruby's parallel assignment operator. If the last lvalue is preceded by an asterisk, all the remaining rvalues will be collected and assigned to that lvalue as an array. Similarly, if the last rvalue is an array, you can prefix it with an asterisk, which effectively expands it into its constituent values in place. (This is not necessary if the rvalue is the only thing on the right-hand side---the array will be expanded automatically.) a = [1, 2, 3, 4] b, c = a » b == 1, b, *c = a » b == 1, b, c = 99, a » b == 99, b, *c = 99, a » b == 99, b, c = 99, *a » b == 99, b, *c = 99, *a » b == 99,
c == 2 c == [2, 3, 4] c == [1, 2, 3, 4] c == [[1, 2, 3, 4]] c == 1 c == [1, 2, 3, 4]
http://www.rubycentral.com/book/tut_expressions.html (6 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Nested Assignments Parallel assignments have one more feature worth mentioning. The left-hand side of an assignment may contain a parenthesized list of terms. Ruby treats these terms as if they were a nested assignment statement. It extracts out the corresponding rvalue, assigning it to the parenthesized terms, before continuing with the higher-level assignment. b, (c, d), e = 1,2,3,4 b, (c, d), e = [1,2,3,4] b, (c, d), e = 1,[2,3],4 b, (c, d), e = 1,[2,3,4],5 b, (c,*d), e = 1,[2,3,4],5
» » » » »
b == 1, b == 1, b == 1, b == 1, b == 1,
c == 2, c == 2, c == 2, c == 2, c == 2,
d == nil, d == nil, d == 3, d == 3, d == [3, 4],
e == 3 e == 3 e == 4 e == 5 e == 5
Other Forms of Assignment In common with many other languages, Ruby has a syntactic shortcut: a=a+2 may be written as a+=2. The second form is converted internally to the first. This means that operators that you have defined as methods in your own classes work as you'd expect. class Bowdlerize def initialize(aString) @value = aString.gsub(/[aeiou]/, '*') end def +(other) Bowdlerize.new(self.to_s + other.to_s) end def to_s @value end end a = Bowdlerize.new("damn ") a += "shame"
» d*mn » d*mn sh*m*
Conditional Execution Ruby has several different mechanisms for conditional execution of code; most of them should feel familiar, and many have some neat twists. Before we get into them, though, we need to spend a short time looking at boolean expressions.
http://www.rubycentral.com/book/tut_expressions.html (7 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Boolean Expressions Ruby has a simple definition of truth. Any value that is not nil or the constant false is true. You'll find that the library routines use this fact consistently. For example, IO#gets, which returns the next line from a file, returns nil at end of file, enabling you to write loops such as: while line = gets # process line end However, there's a trap here for C, C++, and Perl programmers. The number zero is not interpreted as a false value. Neither is a zero-length string. This can be a tough habit to break.
Defined?, And, Or, and Not Ruby supports all the standard boolean operators and introduces the new operator defined?. Both ``and'' and ``&&'' evaluate to true only if both operands are true. They evaluate the second operand only if the first is true (this is sometimes known as ``short-circuit evaluation''). The only difference in the two forms is precedence (``and'' binds lower than ``&&''). Similarly, both ``or'' and ``||'' evaluate to true if either operand is true. They evaluate their second operand only if the first is false. As with ``and'', the only difference between ``or'' and ``||'' is their precedence. Just to make life interesting, ``and'' and ``or'' have the same precedence, while ``&&'' has a higher precedence than ``||''. ``not'' and ``!'' return the opposite of their operand (false if the operand is true, and true if the operand is false). And, yes, ``not'' and ``!'' differ only in precedence. All these precedence rules are summarized in Table 18.4 on page 221. The defined? operator returns nil if its argument (which can be an arbitrary expression) is not defined, otherwise it returns a description of that argument. If the argument is yield, defined? returns the string ``yield'' if a code block is associated with the current context. defined? defined? defined? defined? defined? defined? defined? defined? defined?
1 dummy printf String $& $_ Math::PI ( c,d = 1,2 ) 42.abs
» » » » » » » » »
"expression" nil "method" "constant" "$&" "global-variable" "constant" "assignment" "method"
In addition to the boolean operators, Ruby objects support comparison using the methods ==, ===, , =~, eql?, and equal? (see Table 7.1 on page 81). All but are defined in class Object but are http://www.rubycentral.com/book/tut_expressions.html (8 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
often overridden by descendents to provide appropriate semantics. For example, class Array redefines == so that two array objects are equal if they have the same number of elements and corresponding elements are equal. Common comparison operators Operator == ===
Meaning Test for equal value. Used to test equality within a when clause of a case statement. General comparison operator. Returns -1, 0, or +1, depending on whether its receiver is less than, equal to, or greater than its argument. Comparison operators for less than, less than or equal, greater than or equal, and greater than. Regular expression pattern match. =~ True if the receiver and argument have both the same type and equal eql? values. 1 == 1.0 returns true, but 1.eql?(1.0) is false. True if the receiver and argument have the same object id. equal? Both == and =~ have negated forms, != and !~. However, these are converted by Ruby when your program is read. a!=b is equivalent to !(a==b), and a!~b is the same as !(a=~b). This means that if you write a class that overrides == or =~ you get a working != and !~ for free. But on the flip side, this also means that you cannot define != and !~ independent of == and =~, respectively. You can use a Ruby range as a boolean expression. A range such as exp1..exp2 will evaluate as false until exp1 becomes true. The range will then evaluate as true until exp2 becomes true. Once this happens, the range resets, ready to fire again. We show some examples of this on page 84. Finally, you can use a bare regular expression as a boolean expression. Ruby expands it to $_=~/re/.
If and Unless Expressions An if expression in Ruby is pretty similar to ``if'' statements in other languages. if aSong.artist == "Gillespie" then handle = "Dizzy" elsif aSong.artist == "Parker" then handle = "Bird" else handle = "unknown" end If you lay out your if statements on multiple lines, you can leave off the then keyword.
http://www.rubycentral.com/book/tut_expressions.html (9 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
if aSong.artist == "Gillespie" handle = "Dizzy" elsif aSong.artist == "Parker" handle = "Bird" else handle = "unknown" end However, if you lay your code out more tightly, the then keyword is necessary to separate the boolean expression from the following statements. if aSong.artist == "Gillespie" then elsif aSong.artist == "Parker" then else handle = "unknown" end
handle = "Dizzy" handle = "Bird"
You can have zero or more elsif clauses and an optional else clause. As we've said before, if is an expression, not a statement---it returns a value. You don't have to use the value of an if expression, but it can come in handy. handle = if aSong.artist == "Gillespie" then "Dizzy" elsif aSong.artist == "Parker" then "Bird" else "unknown" end Ruby also has a negated form of the if statement: unless aSong.duration > 180 then cost = .25 else cost = .35 end Finally, for the C fans out there, Ruby also supports the C-style conditional expression: cost = aSong.duration > 180 ? .35 : .25 The conditional expression returns the value of either the expression before or the expression after the colon, depending on whether the boolean expression before the question mark evaluates to true or false. In this case, if the song duration is greater than 3 minutes, the expression returns .35. For shorter songs, it returns .25. Whatever the result, it is then assigned to cost.
http://www.rubycentral.com/book/tut_expressions.html (10 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
If and Unless Modifiers Ruby shares a neat feature with Perl. Statement modifiers let you tack conditional statements onto the end of a normal statement. mon, day, year = $1, $2, $3 if /(\d\d)-(\d\d)-(\d\d)/ puts "a = #{a}" if fDebug print total unless total == 0 For an if modifier, the preceding expression will be evaluated only if the condition is true. unless works the other way around. while gets next if /^#/ parseLine unless /^$/ end
# Skip comments # Don't parse empty lines
Because if itself is an expression, you can get really obscure with statements such as: if artist == "John Coltrane" artist = "'Trane" end unless nicknames == "no" This path leads to the gates of madness.
Case Expressions The Ruby case expression is a powerful beast: a multiway if on steroids. case inputLine when "debug" dumpDebugInfo dumpSymbols when /p\s+(\w+)/ dumpVariable($1) when "quit", "exit" exit else print "Illegal command: #{inputLine}" end http://www.rubycentral.com/book/tut_expressions.html (11 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
As with if, case returns the value of the last expression executed, and you also need a then keyword if the expression is on the same line as the condition. kind = case year when 1850..1889 when 1890..1909 when 1910..1929 when 1930..1939 when 1940..1950 else end
then then then then then
"Blues" "Ragtime" "New Orleans Jazz" "Swing" "Bebop" "Jazz"
case operates by comparing the target (the expression after the keyword case) with each of the comparison expressions after the when keywords. This test is done using comparison === target. As long as a class defines meaningful semantics for === (and all the built-in classes do), objects of that class can be used in case expressions. For example, regular expressions define === as a simple pattern match. case line when /title=(.*)/ puts "Title is #$1" when /track=(.*)/ puts "Track is #$1" when /artist=(.*)/ puts "Artist is #$1" end Ruby classes are instances of class Class, which defines === as a test to see if the argument is an instance of the class or one of its superclasses. So (abandoning the benefits of polymorphism and bringing the gods of refactoring down around your ears), you can test the class of objects: case shape when Square, Rectangle # ... when Circle # ... when Triangle # ... else # ... end
http://www.rubycentral.com/book/tut_expressions.html (12 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Loops Don't tell anyone, but Ruby has pretty primitive built-in looping constructs. The while loop executes its body zero or more times as long as its condition is true. For example, this common idiom reads until the input is exhausted. while gets # ... end There's also a negated form that executes the body until the condition becomes true. until playList.duration > 60 playList.add(songList.pop) end As with if and unless, both of the loops can also be used as statement modifiers. a *= 2 while a < 100 a -= 10 until a < 100 On page 80 in the section on boolean expressions, we said that a range can be used as a kind of flip-flop, returning true when some event happens and then staying true until a second event occurs. This facility is normally used within loops. In the example that follows, we read a text file containing the first ten ordinal numbers (``first,'' ``second,'' and so on) but only print the lines starting with the one that matches ``third'' and ending with the one that matches ``fifth.'' file = File.open("ordinal") while file.gets print if /third/ .. /fifth/ end produces: third fourth fifth The elements of a range used in a boolean expression can themselves be expressions. These are evaluated each time the overall boolean expression is evaluated. For example, the following code uses the fact that the variable $. contains the current input line number to display line numbers one through three and those between a match of /eig/ and /nin/.
http://www.rubycentral.com/book/tut_expressions.html (13 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
file = File.open("ordinal") while file.gets print if ($. == 1) || /eig/ .. ($. == 3) || /nin/ end produces: first second third eighth ninth There's one wrinkle when while and until are used as statement modifiers. If the statement they are modifying is a begin/end block, the code in the block will always execute at least one time, regardless of the value of the boolean expression. print "Hello\n" while false begin print "Goodbye\n" end while false produces: Goodbye
Iterators If you read the beginning of the previous section, you might have been discouraged. ``Ruby has pretty primitive built-in looping constructs,'' it said. Don't despair, gentle reader, for there's good news. Ruby doesn't need any sophisticated built-in loops, because all the fun stuff is implemented using Ruby iterators. For example, Ruby doesn't have a ``for'' loop---at least not the kind you'd find in C, C++, and Java. Instead, Ruby uses methods defined in various built-in classes to provide equivalent, but less error-prone, functionality. Let's look at some examples. 3.times do print "Ho! " end produces: Ho! Ho! Ho! http://www.rubycentral.com/book/tut_expressions.html (14 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
It's easy to avoid fencepost and off-by-1 errors; this loop will execute three times, period. In addition to times, integers can loop over specific ranges by calling downto, upto, and step. For instance, a traditional ``for'' loop that runs from 0 to 9 (something like i=0; i < 10; i++) is written as follows. 0.upto(9) do |x| print x, " " end produces: 0 1 2 3 4 5 6 7 8 9 A loop from 0 to 12 by 3 can be written as follows. 0.step(12, 3) {|x| print x, " " } produces: 0 3 6 9 12 Similarly, iterating over arrays and other containers is made easy using their each method. [ 1, 1, 2, 3, 5 ].each {|val| print val, " " } produces: 1 1 2 3 5 And once a class supports each, the additional methods in the Enumerable module (documented beginning on page 407 and summarized on pages 104--105) become available. For example, the File class provides an each method, which returns each line of a file in turn. Using the grep method in Enumerable, we could iterate over only those lines that meet a certain condition. File.open("ordinal").grep /d$/ do |line| print line end produces: second third Last, and probably least, is the most basic loop of all. Ruby provides a built-in iterator called loop.
http://www.rubycentral.com/book/tut_expressions.html (15 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
loop { # block ... } The loop iterator calls the associated block forever (or at least until you break out of the loop, but you'll have to read ahead to find out how to do that).
For ... In Earlier we said that the only built-in Ruby looping primitives were while and until. What's this ``for'' thing, then? Well, for is almost a lump of syntactic sugar. When you write for aSong in songList aSong.play end Ruby translates it into something like: songList.each do |aSong| aSong.play end The only difference between the for loop and the each form is the scope of local variables that are defined in the body. This is discussed on page 89. You can use for to iterate over any object that responds to the method each, such as an Array or a Range. for i in ['fee', 'fi', 'fo', 'fum'] print i, " " end for i in 1..3 print i, " " end for i in File.open("ordinal").find_all { |l| l =~ /d$/} print i.chomp, " " end produces: fee fi fo fum 1 2 3 second third As long as your class defines a sensible each method, you can use a for loop to traverse it.
http://www.rubycentral.com/book/tut_expressions.html (16 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class Periods def each yield "Classical" yield "Jazz" yield "Rock" end end periods = Periods.new for genre in periods print genre, " " end produces: Classical Jazz Rock
Break, Redo, and Next The loop control constructs break, redo, and next let you alter the normal flow through a loop or iterator. break terminates the immediately enclosing loop; control resumes at the statement following the block. redo repeats the loop from the start, but without reevaluating the condition or fetching the next element (in an iterator). next skips to the end of the loop, effectively starting the next iteration. while gets next if /^\s*#/ break if /^END/
# skip comments # stop at end # substitute stuff in backticks and try again redo if gsub!(/`(.*?)`/) { eval($1) } # process line ... end These keywords can also be used with any of the iterator-based looping mechanisms: i=0 loop do i += 1 next if i < 3 print i break if i > 4 end produces: http://www.rubycentral.com/book/tut_expressions.html (17 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
345
Retry The redo statement causes a loop to repeat the current iteration. Sometimes, though, you need to wind the loop right back to the very beginning. The retry statement is just the ticket. retry restarts any kind of iterator loop. for i in 1..100 print "Now at #{i}. Restart? " retry if gets =~ /^y/i end Running this interactively, you might see Now at 1. Restart? n Now at 2. Restart? y Now at 1. Restart? n . . . retry will reevaluate any arguments to the iterator before restarting it. The online Ruby documentation has the following example of a do-it-yourself until loop. def doUntil(cond) yield retry unless cond end i = 0 doUntil(i > 3) { print i, " " i += 1 } produces: 0 1 2 3 4
Variable Scope and Loops The while, until, and for loops are built into the language and do not introduce new scope; previously existing locals can be used in the loop, and any new locals created will be available afterward. The blocks used by iterators (such as loop and each) are a little different. Normally, the local variables http://www.rubycentral.com/book/tut_expressions.html (18 of 19) [7/20/2001 9:49:05 PM]
Programming Ruby: The Pragmatic Programmer's Guide
created in these blocks are not accessible outside the block. [ 1, 2, 3 ].each do |x| y = x + 1 end [ x, y ] produces: prog.rb:4: undefined local variable or method `x' for # (NameError) However, if at the time the block executes a local variable already exists with the same name as that of a variable in the block, the existing local variable will be used in the block. Its value will therefore be available after the block finishes. As the following example shows, this applies both to normal variables in the block and to the block's parameters. x = nil y = nil [ 1, 2, 3 ].each do |x| y = x + 1 end [ x, y ]
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Exceptions, Catch, and Throw So far we're been developing code in Pleasantville, a wonderful place where nothing ever, ever goes wrong. Every library call succeeds, users never enter incorrect data, and resources are plentiful and cheap. Well, that's about to change. Welcome to the real world! In the real world, errors happen. Good programs (and programmers) anticipate them and arrange to handle them gracefully. This isn't always as easy as it might be. Often the code that detects an error does not have the context to know what to do about it. For example, attempting to open a file that doesn't exist is acceptable in some circumstances and is a fatal error at other times. What's your file-handling module to do? The traditional approach is to use return codes. The open method returns some specific value to say it failed. This value is then propagated back through the layers of calling routines until someone wants to take responsibility for it. The problem with this approach is that managing all these error codes can be a pain. If a function calls open, then read, and finally close, and each can return an error indication, how can the function distinguish these error codes in the value it returns to its caller? To a large extent, exceptions solve this problem. Exceptions let you package up information about an error into an object. That exception object is then propagated back up the calling stack automatically until the runtime system finds code that explicitly declares that it knows how to handle that type of exception.
The Exception Class The package that contains the information about an exception is an object of class Exception, or one of class Exception's children. Ruby predefines a tidy hierarchy of exceptions, shown in Figure 8.1 on page 93. As we'll see later, this hierarchy makes handling exceptions considerably easier. Figure not available... When you need to raise an exception, you can use one of the built-in Exception classes, or you can create one of your own. If you create your own, you might want to make it a subclass of StandardError or one of its children. If you don't, your exception won't be caught by default. http://www.rubycentral.com/book/tut_exceptions.html (1 of 7) [7/20/2001 9:49:06 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Every Exception has associated with it a message string and a stack backtrace. If you define your own exceptions, you can add additional information.
Handling Exceptions Our jukebox downloads songs from the Internet using a TCP socket. The basic code is simple: opFile = File.open(opName, "w") while data = socket.read(512) opFile.write(data) end What happens if we get a fatal error halfway through the download? We certainly don't want to store an incomplete song in the song list. ``I Did It My *click*''. Let's add some exception handling code and see how it helps. We enclose the code that could raise an exception in a begin/end block and use rescue clauses to tell Ruby the types of exceptions we want to handle. In this case we're interested in trapping SystemCallError exceptions (and, by implication, any exceptions that are subclasses of SystemCallError), so that's what appears on the rescue line. In the error handling block, we report the error, close and delete the output file, and then reraise the exception. opFile = File.open(opName, "w") begin # Exceptions raised by this code will # be caught by the following rescue clause while data = socket.read(512) opFile.write(data) end rescue SystemCallError $stderr.print "IO failed: " + $! opFile.close File.delete(opName) raise end When an exception is raised, and independent of any subsequent exception handling, Ruby places a reference to the Exception object associated with the exception in the global variable $! (the exclamation point presumably mirroring our surprise that any of our code could cause errors). In the previous example, we used this variable to format our error message. After closing and deleting the file, we call raise with no parameters, which reraises the exception in $!. This is a useful technique, as it allows you to write code that filters exceptions, passing on those you can't handle to higher levels. It's almost like implementing an inheritance hierarchy for error processing. You can have multiple rescue clauses in a begin block, and each rescue clause can specify
http://www.rubycentral.com/book/tut_exceptions.html (2 of 7) [7/20/2001 9:49:06 PM]
Programming Ruby: The Pragmatic Programmer's Guide
multiple exceptions to catch. At the end of each rescue clause you can give Ruby the name of a local variable to receive the matched exception. Many people find this more readable than using $! all over the place. begin eval string rescue SyntaxError, NameError => boom print "String doesn't compile: " + boom rescue StandardError => bang print "Error running script: " + bang end How does Ruby decide which rescue clause to execute? It turns out that the processing is pretty similar to that used by the case statement. For each rescue clause in the begin block, Ruby compares the raised exception against each of the parameters in turn. If the raised exception matches a parameter, Ruby executes the body of the rescue and stops looking. The match is made using $!.kind_of?(parameter), and so will succeed if the parameter has the same class as the exception or is an ancestor of the exception. If you write a rescue clause with no parameter list, the parameter defaults to StandardError. If no rescue clause matches, or if an exception is raised outside a begin/end block, Ruby moves up the stack and looks for an exception handler in the caller, then in the caller's caller, and so on. Although the parameters to the rescue clause are typically the names of Exception classes, they can actually be arbitrary expressions (including method calls) that return an Exception class.
Tidying Up Sometimes you need to guarantee that some processing is done at the end of a block of code, regardless of whether an exception was raised. For example, you may have a file open on entry to the block, and you need to make sure it gets closed as the block exits. The ensure clause does just this. ensure goes after the last rescue clause and contains a chunk of code that will always be executed as the block terminates. It doesn't matter if the block exits normally, if it raises and rescues an exception, or if it is terminated by an uncaught exception---the ensure block will get run. f = File.open("testfile") begin # .. process rescue # .. handle error ensure f.close unless f.nil? end The else clause is a similar, although less useful, construct. If present, it goes after the rescue clauses and before any ensure. The body of an else clause is executed only if no exceptions are raised by the http://www.rubycentral.com/book/tut_exceptions.html (3 of 7) [7/20/2001 9:49:06 PM]
Programming Ruby: The Pragmatic Programmer's Guide
main body of code. f = File.open("testfile") begin # .. process rescue # .. handle error else puts "Congratulations-- no errors!" ensure f.close unless f.nil? end
Play It Again Sometimes you may be able to correct the cause of an exception. In those cases, you can use the retry statement within a rescue clause to repeat the entire begin/end block. Clearly there is tremendous scope for infinite loops here, so this is a feature to use with caution (and with a finger resting lightly on the interrupt key). As an example of code that retries on exceptions, have a look at the following, adapted from Minero Aoki's net/smtp.rb library. @esmtp = true begin # First try an extended login. If it fails because the # server doesn't support it, fall back to a normal login if @esmtp then @command.ehlo(helodom) else @command.helo(helodom) end rescue ProtocolError if @esmtp then @esmtp = false retry else raise end end This code tries first to connect to an SMTP server using the EHLO command, which is not universally http://www.rubycentral.com/book/tut_exceptions.html (4 of 7) [7/20/2001 9:49:06 PM]
Programming Ruby: The Pragmatic Programmer's Guide
supported. If the connection attempt fails, the code sets the @esmtp variable to false and retries the connection. If this fails again, the exception is reraised up to the caller.
Raising Exceptions So far we've been on the defensive, handling exceptions raised by others. It's time to turn the tables and go on the offensive. (There are those that say your gentle authors are always offensive, but that's a different book.) You can raise exceptions in your code with the Kernel::raise method. raise raise "bad mp3 encoding" raise InterfaceException, "Keyboard failure", caller The first form simply reraises the current exception (or a RuntimeError if there is no current exception). This is used in exception handlers that need to intercept an exception before passing it on. The second form creates a new RuntimeError exception, setting its message to the given string. This exception is then raised up the call stack. The third form uses the first argument to create an exception and then sets the associated message to the second argument and the stack trace to the third argument. Typically the first argument will be either the name of a class in the Exception hierarchy or a reference to an object instance of one of these classes.[Technically, this argument can be any object that responds to the message exception by returning an object such that object.kind_of?(Exception) is true.] The stack trace is normally produced using the Kernel::caller method. Here are some typical examples of raise in action. raise raise "Missing name" if name.nil? if i >= myNames.size raise IndexError, "#{i} >= size (#{myNames.size})" end raise ArgumentError, "Name too big", caller In the last example, we remove the current routine from the stack backtrace, which is often useful in library modules. We can take this further: the following code removes two routines from the backtrace. raise ArgumentError, "Name too big", caller[1..-1]
http://www.rubycentral.com/book/tut_exceptions.html (5 of 7) [7/20/2001 9:49:06 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Adding Information to Exceptions You can define your own exceptions to hold any information that you need to pass out from the site of an error. For example, certain types of network errors might be transient depending on the circumstances. If such an error occurs, and the circumstances are right, you could set a flag in the exception to tell the handler that it might be worth retrying the operation. class RetryException < RuntimeError attr :okToRetry def initialize(okToRetry) @okToRetry = okToRetry end end Somewhere down in the depths of the code, a transient error occurs. def readData(socket) data = socket.read(512) if data.nil? raise RetryException.new(true), "transient read error" end # .. normal processing end Higher up the call stack, we handle the exception. begin stuff = readData(socket) # .. process stuff rescue RetryException => detail retry if detail.okToRetry raise end
Catch and Throw While the exception mechanism of raise and rescue is great for abandoning execution when things go wrong, it's sometimes nice to be able to jump out of some deeply nested construct during normal processing. This is where catch and throw come in handy.
http://www.rubycentral.com/book/tut_exceptions.html (6 of 7) [7/20/2001 9:49:06 PM]
Programming Ruby: The Pragmatic Programmer's Guide
catch (:done) do while gets throw :done unless fields = split(/\t/) songList.add(Song.new(*fields)) end songList.play end catch defines a block that is labeled with the given name (which may be a Symbol or a String). The block is executed normally until a throw is encountered. When Ruby encounters a throw, it zips back up the call stack looking for a catch block with a matching symbol. When it finds it, Ruby unwinds the stack to that point and terminates the block. If the throw is called with the optional second parameter, that value is returned as the value of the catch. So, in the previous example, if the input does not contain correctly formatted lines, the throw will skip to the end of the corresponding catch, not only terminating the while loop but also skipping the playing of the song list. The following example uses a throw to terminate interaction with the user if ``!'' is typed in response to any prompt. def promptAndGet(prompt) print prompt res = readline.chomp throw :quitRequested if res == "!" return res end catch :quitRequested do name = promptAndGet("Name: ") age = promptAndGet("Age: ") sex = promptAndGet("Sex: ") # .. # process information end As this example illustrates, the throw does not have to appear within the static scope of the catch. Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: foreach new pipe popen readlines select anArray Creates a pair of pipe endpoints (connected to each other) and returns them as a two-element array of IO objects: [ readFile, writeFile ]. Not available on all platforms. In the example below, the two processes close the ends of the pipe that they are not using. This is not just a cosmetic nicety. The read end of a pipe will not generate an end of file condition if there are any writers with the pipe still open. In the case of the parent process, the rd.read will never return if it does not first issue a wr.close. rd, wr = IO.pipe if fork wr.close puts "Parent got: " rd.close Process.wait else rd.close puts "Sending message to parent" wr.write "Hi Dad" wr.close end produces: Sending message to parent Parent got:
popen
IO.popen( aCmdString, aModeString="r" ) -> anIO IO.popen( aCmdString, aModeString="r" ) {| anIO | block } -> nil
http://www.rubycentral.com/book/ref_c_io.html (3 of 14) [7/20/2001 9:49:08 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Runs the specified command string as a subprocess; the subprocess's standard input and output will be connected to the returned IO object. If aCmdString starts with a ``-'', then a new instance of Ruby is started as the subprocess. The default mode for the new file object is ``r'', but aModeString may be set to any of the modes in Table 22.5 on page 331. If a block is given, Ruby will run the command as a child connected to Ruby with a pipe. Ruby's end of the pipe will be passed as a parameter to the block. If a block is given with a aCmdString of ``-'', the block will be run in two separate processes: once in the parent, and once in a child. The parent process will be passed the pipe object as a parameter to the block, the child version of the block will be passed nil, and the child's standard in and standard out will be connected to the parent through the pipe. Not available on all platforms. f = IO.popen("uname") p f.readlines puts "Parent is #{Process.pid}" IO.popen ("date") { |f| puts f.gets } IO.popen("-") {|f| $stderr.puts "#{Process.pid} is here, f is #{f}"} produces: ["Linux\n"] Parent is 15506 Sun Mar 4 23:28:51 CST 2001 15509 is here, f is 15506 is here, f is #
readlines
IO.readlines( aPortName, aSepString=$/ ) -> anArray Reads the entire file specified by aPortName as individual lines, and returns those lines in an array. Lines are separated by aSepString. a = IO.readlines("testfile") » "This is line one\n" a[0]
select
IO.select( readArray [, writeArray [errorArray [timeout]]] ) -> anArray or nil See Kernel#select on page 426.
instance methods
nil
http://www.rubycentral.com/book/ref_c_io.html (5 of 14) [7/20/2001 9:49:08 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Closes the write end of a duplex I/O stream (i.e., one that contains both a read and a write stream, such as a pipe). Will raise an IOError if the stream is not duplexed. f = IO.popen("/bin/sh","r+") f.close_write f.print "nowhere" produces: prog.rb:3:in `write': not opened for writing (IOError) from prog.rb:3:in `print' from prog.rb:3
closed?
ios.closed? -> true or false Returns true if ios is completely closed (for duplex streams, both reader and writer), false otherwise. f = File.new("testfile") f.close f.closed? f = IO.popen("/bin/sh","r+") f.close_write f.closed? f.close_read f.closed?
each
» »
nil true
» » » »
nil false nil true
ios.each( aSepString=$/ ) {| line | block } -> ios Executes the block for every line in ios, where lines are separated by aSepString. ios must be opened for reading or an IOerror will be raised. f = File.new("testfile") f.each {|line| puts "#{f.lineno}: #{line}" } produces: 1: 2: 3: 4:
each_byte
This is line one This is line two This is line three And so on...
ios.each_byte {| byte | block } -> nil
http://www.rubycentral.com/book/ref_c_io.html (6 of 14) [7/20/2001 9:49:08 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Calls the given block once for each byte (0..255) in ios, passing the byte as an argument. The stream must be opened for reading or an IOerror will be raised. f = File.new("testfile") checksum = 0 f.each_byte {|x| checksum ^= x } checksum
each_line
» nil » 12
ios.each_line( aSepString=$/ ) {| line | block } -> ios Synonym for IO#each.
eof
ios.eof -> true or false Returns true if ios is at end of file. The stream must be opened for reading or an IOError will be raised. f = File.new("testfile") dummy = f.readlines f.eof
eof?
»
true
ios.eof? -> true or false Synonym for IO#eof.
fcntl
ios.fcntl( anIntegerCmd, anArg ) -> anInteger Provides a mechanism for issuing low-level commands to control or query file-oriented I/O streams. Arguments and results are platform dependent. If anArg is a number, its value is passed directly. If it is a string, it is interpreted as a binary sequence of bytes. On Unix platforms, see fcntl(2) for details. Not implemented on all platforms.
fileno
ios.fileno -> aFixnum Returns an integer representing the numeric file descriptor for ios. $stdin.fileno $stdout.fileno
flush
ios.flush -> ios
http://www.rubycentral.com/book/ref_c_io.html (7 of 14) [7/20/2001 9:49:08 PM]
» »
0 1
Programming Ruby: The Pragmatic Programmer's Guide
Flushes any buffered data within ios to the underlying operating system (note that this is Ruby internal buffering only; the OS may buffer the data as well). $stdout.print "no newline" $stdout.flush produces: no newline
getc
ios.getc -> aFixnum or nil Gets the next 8-bit byte (0..255) from ios. Returns nil if called at end of file. f = File.new("testfile") f.getc f.getc
gets
» »
84 104
ios.gets( aSepString=$/ ) -> aString or nil Reads the next ``line'' from the I/O stream; lines are separated by aSepString. A separator of nil reads the entire contents, and a zero-length separator reads the input a paragraph at a time (two successive newlines in the input separate paragraphs). The stream must be opened for reading or an IOerror will be raised. The line read in will be returned and also assigned to $_. Returns nil if called at end of file. File.new("testfile").gets $_
ioctl
» "This is line one\n" » "This is line one\n"
ios.ioctl( anIntegerCmd, anArg ) -> anInteger Provides a mechanism for issuing low-level commands to control or query I/O devices. Arguments and results are platform dependent. If anArg is a number, its value is passed directly. If it is a string, it is interpreted as a binary sequence of bytes. On Unix platforms, see ioctl(2) for details. Not implemented on all platforms.
isatty
ios.isatty -> true or false Returns true if ios is associated with a terminal device (tty), false otherwise. File.new("testfile").isatty File.new("/dev/tty").isatty
lineno
ios.lineno -> anInteger
http://www.rubycentral.com/book/ref_c_io.html (8 of 14) [7/20/2001 9:49:08 PM]
» false » true
Programming Ruby: The Pragmatic Programmer's Guide
Returns the current line number in ios. The stream must be opened for reading. lineno counts the number of times gets is called, rather than the number of newlines encountered. The two values will differ if gets is called with a separator other than newline. See also the $. variable. f = File.new("testfile") » 0 f.lineno » "This is line one\n" f.gets » 1 f.lineno » "This is line two\n" f.gets » 2 f.lineno
lineno=
ios.lineno = anInteger -> anInteger Manually sets the current line number to the given value. $. is updated only on the next read. f = File.new("testfile") f.gets $. f.lineno = 1000 f.lineno $. # lineno of last read f.gets $. # lineno of last read
pid
» "This is line one\n" » 1 » » » »
1000 1 "This is line two\n" 1001
ios.pid -> aFixnum Returns the process ID of a child process associated with ios. This will be set by IO::popen. pipe = IO.popen("-") if pipe $stderr.puts "In parent, child pid is #{pipe.pid}" else $stderr.puts "In child, pid is #{$$}" end produces: In parent, child pid is 15545 In child, pid is 15545
pos
ios.pos -> anInteger
http://www.rubycentral.com/book/ref_c_io.html (9 of 14) [7/20/2001 9:49:08 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns the current offset (in bytes) of ios. f = File.new("testfile") » 0 f.pos » "This is line one\n" f.gets » 17 f.pos
pos=
ios.pos = anInteger -> 0 Seeks to the given position (in bytes) in ios. f = File.new("testfile") f.pos = 17 » "This is line two\n" f.gets
print
ios.print( [anObject=$_]* ) -> nil Writes the given object(s) to ios. The stream must be opened for writing. If the output record separator ($\) is not nil, it will be appended to the output. If no arguments are given, prints $_. Objects that aren't strings will be converted by calling their to_s method. Returns nil. $stdout.print("This is ", 100, " percent.\n") produces: This is 100 percent.
printf
ios.printf( aFormatString [, anObject]* ) -> nil Formats and writes to ios, converting parameters under control of the format string. See Kernel#sprintf on page 427 for details.
putc
ios.putc( anObject ) -> anObject Writes the given character (taken from a String or a Fixnum) on ios. $stdout.putc "A" $stdout.putc 65 produces: AA
puts
ios.puts( [anObject]* ) -> nil
http://www.rubycentral.com/book/ref_c_io.html (10 of 14) [7/20/2001 9:49:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Writes the given objects to ios as with IO#print. Writes a record separator (typically a newline) after any that do not already end with a newline sequence. If called with an array argument, writes each element on a new line. If called without arguments, outputs a single record separator. $stdout.puts("this", "is", "a", "test") produces: this is a test
read
ios.read( [anInteger] ) -> aString or nil Reads at most anInteger bytes from the I/O stream, or to the end of file if anInteger is omitted. Returns nil if called at end of file. f = File.new("testfile") » "This is line one" f.read(16)
readchar
ios.readchar -> aFixnum Reads a character as with IO#getc, but raises an EOFError on end of file.
readline
ios.readline( aSepString=$/ ) -> aString Reads a line as with IO#gets, but raises an EOFError on end of file.
readlines
ios.readlines( aSepString=$/ ) -> anArray Reads all of the lines in ios, and returns them in anArray. Lines are separated by the optional aSepString. The stream must be opened for reading or an IOerror will be raised. f = File.new("testfile") » "This is line one\n" f.readlines[0]
reopen
ios.reopen( anOtherIO ) -> ios ios.reopen( aPath, aModeStr ) -> ios
http://www.rubycentral.com/book/ref_c_io.html (11 of 14) [7/20/2001 9:49:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Reassociates ios with the I/O stream given in anOtherIO or to a new stream opened on aPath. This may dynamically change the actual class of this stream. f1 = File.new("testfile") f2 = File.new("testfile") » "This is line one\n" f2.readlines[0] » # f2.reopen(f1) » "This is line one\n" f2.readlines[0]
rewind
ios.rewind -> 0 Positions ios to the beginning of input, resetting lineno to zero. f = File.new("testfile") » "This is line one\n" f.readline » 0 f.rewind » 0 f.lineno » "This is line one\n" f.readline
seek
ios.seek( anInteger, whence=SEEK_SET ) -> 0 Seeks to a given offset anInteger in the stream according to the value of whence: IO::SEEK_CUR Seeks to anInteger plus current position. IO::SEEK_END Seeks to anInteger plus end of stream (you probably want a negative value for anInteger). IO::SEEK_SET Seeks to the absolute location given by anInteger. f = File.new("testfile") f.seek(-13, IO::SEEK_END) f.readline
stat
» 0 » "And so on...\n"
ios.stat -> aStat Returns status information for ios as an object of type File::Stat. f = File.new("testfile") s = f.stat » "100644" "%o" % s.mode » 4096 s.blksize » Sun Mar 04 23:28:52 CST 2001 s.atime
sync
ios.sync -> true or false
http://www.rubycentral.com/book/ref_c_io.html (12 of 14) [7/20/2001 9:49:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns the current ``sync mode'' of ios. When sync mode is true, all output is immediately flushed to the underlying operating system and is not buffered by Ruby internally. f = File.new("testfile") f.sync
sync=
»
false
ios.sync = aBoolean -> aBoolean Sets the ``sync mode'' to true or false. When sync mode is true, all output is immediately flushed to the underlying operating system and is not buffered internally. Returns the new state. f = File.new("testfile") f.sync = true
sysread
ios.sysread( anInteger ) -> aString Reads anInteger bytes from ios using a low-level read and returns them as a string. Do not mix with other methods that read from ios or you may get unpredictable results. Raises SystemCallError on error and EOFError at end of file. f = File.new("testfile") » "This is line one" f.sysread(16)
syswrite
ios.syswrite( aString ) -> anInteger Writes the given string to ios using a low-level write. Returns the number of bytes written. Do not mix with other methods that write to ios or you may get unpredictable results. Raises SystemCallError on error. f = File.new("out", "w") f.syswrite("ABCDEF")
tell
ios.tell -> anInteger Synonym for IO#pos.
to_i
ios.to_i -> anInteger Synonym for IO#fileno.
to_io
ios.to_io -> ios Returns ios.
http://www.rubycentral.com/book/ref_c_io.html (13 of 14) [7/20/2001 9:49:09 PM]
»
6
Programming Ruby: The Pragmatic Programmer's Guide
tty?
ios.tty? -> true or false
Synonym for IO#isatty.
ungetc
ios.ungetc( anInteger ) -> nil Pushes back one character onto ios, such that a subsequent buffered read will return it. Only one character may be pushed back before a subsequent read operation (that is, you will be able to read only the last of several characters that have been pushed back). Has no effect with unbuffered reads (such as IO#sysread). f = File.new("testfile") c = f.getc f.ungetc(c) f.getc
write
» » » »
# 84 nil 84
ios.write( aString ) -> anInteger Writes the given string to ios. The stream must be opened for writing. If the argument is not a string, it will be converted to a string using to_s. Returns the number of bytes written. count = $stdout.write( "This is a test\n" ) puts "That was #{count} bytes of data" produces: This is a test That was 15 bytes of data
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Modules Modules are a way of grouping together methods, classes, and constants. Modules give you two major benefits: 1. Modules provide a namespace and prevent name clashes. 2. Modules implement the mixin facility.
Namespaces As you start to write bigger and bigger Ruby programs, you'll naturally find yourself producing chunks of reusable code---libraries of related routines that are generally applicable. You'll want to break this code out into separate files so the contents can be shared among different Ruby programs. Often this code will be organized into classes, so you'll probably stick a class (or a set of interrelated classes) into a file. However, there are times when you want to group things together that don't naturally form a class. An initial approach might be to put all these things into a file and simply load that file into any program that needs it. This is the way the C language works. However, there's a problem. Say you write a set of trigonometry functions sin, cos, and so on. You stuff them all into a file, trig.rb, for future generations to enjoy. Meanwhile, Sally is working on a simulation of good and evil, and codes up a set of her own useful routines, including beGood and sin, and sticks them into action.rb. Joe, who wants to write a program to find out how many angels can dance on the head of a pin, needs to load both trig.rb and action.rb into his program. But both define a method called sin. Bad news. The answer is the module mechanism. Modules define a namespace, a sandbox in which your methods and constants can play without having to worry about being stepped on by other methods and constants. The trig functions can go into one module:
http://www.rubycentral.com/book/tut_modules.html (1 of 8) [7/20/2001 9:49:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
module Trig PI = 3.141592654 def Trig.sin(x) # .. end def Trig.cos(x) # .. end end and the good and bad action methods can go into another: module Action VERY_BAD = 0 BAD = 1 def Action.sin(badness) # ... end end Module constants are named just like class constants, with an initial uppercase letter. The method definitions look similar, too: these module methods are defined just like class methods. If a third program wants to use these modules, it can simply load up the two files (using the Ruby require statement, which we discuss on page 105) and reference the qualified names. require "trig" require "action" y = Trig.sin(Trig::PI/4) wrongdoing = Action.sin(Action::VERY_BAD) As with class methods, you call a module method by preceding its name with the module's name and a period, and you reference a constant using the module name and two colons.
Mixins Modules have another, wonderful use. At a stroke, they pretty much eliminate the need for multiple inheritance, providing a facility called a mixin. In the previous section's examples, we defined module methods, methods whose names were prefixed by the module name. If this made you think of class methods, your next thought might well be ``what happens if I define instance methods within a module?'' Good question. A module can't have instances, because a module isn't a class. However, you can include a module within a class definition. When this happens, all the module's instance methods are suddenly available as methods in the class as well. They get mixed in. In fact, mixed-in modules effectively behave as superclasses. http://www.rubycentral.com/book/tut_modules.html (2 of 8) [7/20/2001 9:49:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
module Debug def whoAmI? "#{self.type.name} (\##{self.id}): #{self.to_s}" end end class Phonograph include Debug # ... end class EightTrack include Debug # ... end ph = Phonograph.new("West End Blues") et = EightTrack.new("Surrealistic Pillow") ph.whoAmI? » "Phonograph (#537683810): West End Blues" et.whoAmI? » "EightTrack (#537683790): Surrealistic Pillow" By including the Debug module, both Phonograph and EightTrack gain access to the whoAmI? instance method. A couple of points about the include statement before we go on. First, it has nothing to do with files. C programmers use a preprocessor directive called #include to insert the contents of one file into another during compilation. The Ruby include statement simply makes a reference to a named module. If that module is in a separate file, you must use require to drag that file in before using include. Second, a Ruby include does not simply copy the module's instance methods into the class. Instead, it makes a reference from the class to the included module. If multiple classes include that module, they'll all point to the same thing. If you change the definition of a method within a module, even while your program is running, all classes that include that module will exhibit the new behavior.[Of course, we're speaking only of methods here. Instance variables are always per-object, for example.] Mixins give you a wonderfully controlled way of adding functionality to classes. However, their true power comes out when the code in the mixin starts to interact with code in the class that uses it. Let's take the standard Ruby mixin Comparable as an example. The Comparable mixin can be used to add the comparison operators (), as well as the method between?, to a class. For this to work, Comparable assumes that any class that uses it defines the operator . So, as a class writer, you define the one method, , include Comparable, and get six comparison functions for free. Let's try this with our Song class, by making the songs comparable based on their duration. All we have to do is include the Comparable module and implement the comparison operator .
http://www.rubycentral.com/book/tut_modules.html (3 of 8) [7/20/2001 9:49:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class Song include Comparable def (other) self.duration other.duration end end We can check that the results are sensible with a few test songs. song1 = Song.new("My Way", "Sinatra", 225) song2 = Song.new("Bicylops", "Fleck", 260) song1 song2 song1 < song2 song1 == song1 song1 > song2
» » » »
-1 true true false
Finally, back on page 45 we showed an implementation of Smalltalk's inject function, implementing it within class Array. We promised then that we'd make it more generally applicable. What better way than making it a mixin module? module Inject def inject(n) each do |value| n = yield(n, value) end n end def sum(initial = 0) inject(initial) { |n, value| n + value } end def product(initial = 1) inject(initial) { |n, value| n * value } end end We can then test this by mixing it into some built-in classes. class Array include Inject end [ 1, 2, 3, 4, 5 ].sum [ 1, 2, 3, 4, 5 ].product class Range include Inject http://www.rubycentral.com/book/tut_modules.html (4 of 8) [7/20/2001 9:49:10 PM]
» 15 » 120
Programming Ruby: The Pragmatic Programmer's Guide
end (1..5).sum (1..5).product ('a'..'m').sum("Letters: ")
» 15 » 120 » "Letters: abcdefghijklm"
For a more extensive example of a mixin, have a look at the documentation for the Enumerable module, which starts on page 407.
Instance Variables in Mixins People coming to Ruby from C++ often ask us, ``What happens to instance variables in a mixin? In C++, I have to jump through some hoops to control how variables are shared in a multiple-inheritance hierarchy. How does Ruby handle this?'' Well, for starters, it's not really a fair question, we tell them. Remember how instance variables work in Ruby: the first mention of an ``@''-prefixed variable creates the instance variable in the current object, self. For a mixin, this means that the module that you mix into your client class (the mixee?) may create instance variables in the client object and may use attr and friends to define accessors for these instance variables. For instance: module Notes attr :concertA def tuning(amt) @concertA = 440.0 + amt end end class Trumpet include Notes def initialize(tune) tuning(tune) puts "Instance method returns #{concertA}" puts "Instance variable is #{@concertA}" end end # The piano is a little flat, so we'll match it Trumpet.new(-5.3) produces: Instance method returns 434.7 Instance variable is 434.7
http://www.rubycentral.com/book/tut_modules.html (5 of 8) [7/20/2001 9:49:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Not only do we have access to the methods defined in the mixin, but we get access to the necessary instance variables as well. There's a risk here, of course, that different mixins may use an instance variable with the same name and create a collision: module MajorScales def majorNum @numNotes = 7 if @numNotes.nil? @numNotes # Return 7 end end module PentatonicScales def pentaNum @numNotes = 5 if @numNotes.nil? @numNotes # Return 5? end end class ScaleDemo include MajorScales include PentatonicScales def initialize puts majorNum # Should be 7 puts pentaNum # Should be 5 end end ScaleDemo.new produces: 7 7 The two bits of code that we mix in both use an instance variable named @numNotes. Unfortunately, the result is probably not what the author intended. For the most part, mixin modules don't try to carry their own instance data around---they use accessors to retrieve data from the client object. But if you need to create a mixin that has to have its own state, ensure that the instance variables have unique names to distinguish them from any other mixins in the system (perhaps by using the module's name as part of the variable name).
http://www.rubycentral.com/book/tut_modules.html (6 of 8) [7/20/2001 9:49:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Iterators and the Enumerable Module You've probably noticed that the Ruby collection classes support a large number of operations that do various things with the collection: traverse it, sort it, and so on. You may be thinking, ``Gee, it'd sure be nice if my class could support all these neat-o features, too!'' (If you actually thought that, it's probably time to stop watching reruns of 1960s television shows.) Well, your classes can support all these neat-o features, thanks to the magic of mixins and module Enumerable. All you have to do is write an iterator called each, which returns the elements of your collection in turn. Mix in Enumerable, and suddenly your class supports things such as map, include?, and find_all?. If the objects in your collection implement meaningful ordering semantics using the method, you'll also get min, max, and sort.
Including Other Files Because Ruby makes it easy to write good, modular code, you'll often find yourself producing small files containing some chunk of self-contained functionality---an interface to x, an algorithm to do y, and so on. Typically, you'll organize these files as class or module libraries. Having produced these files, you'll want to incorporate them into your new programs. Ruby has two statements that do this. load "filename.rb" require "filename" The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once. require has additional functionality: it can load shared binary libraries. Both routines accept relative and absolute paths. If given a relative path (or just a plain name), they'll search every directory in the current load path ($:, discussed on page 142) for the file. Files loaded using load and require can, of course, include other files, which include other files, and so on. What might not be obvious is that require is an executable statement---it may be inside an if statement, or it may include a string that was just built. The search path can be altered at runtime as well. Just add the directory you want to the string $:. Since load will include the source unconditionally, you can use it to reload a source file that may have changed since the program began: 5.times do |i| File.open("temp.rb","w") { |f| f.puts "module Temp\ndef Temp.var() #{i}; end\nend" } load "temp.rb" puts Temp.var end
http://www.rubycentral.com/book/tut_modules.html (7 of 8) [7/20/2001 9:49:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
produces: 0 1 2 3 4
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Basic Input and Output Ruby provides what at first sight looks like two separate sets of I/O routines. The first is the simple interface---we've been using it pretty much exclusively so far. print "Enter your name: " name = gets There are a whole set of I/O-related methods implemented in the Kernel module---gets, open, print, printf, putc, puts, readline, readlines, and test---that make it simple and convenient to write straightforward Ruby programs. These methods typically do I/O to standard input and standard output, which makes them useful for writing filters. You'll find them documented starting on page 415. The second way, which gives you a lot more control, is to use IO objects.
What Is an IO Object? Ruby defines a single base class, IO, to handle input and output. This base class is subclassed by classes File and BasicSocket to provide more specialized behavior, but the principles are the same throughout. An IO object is a bidirectional channel between a Ruby program and some external resource.[For those who just have to know the implementation details, this means that a single IO object can sometimes be managing more than one operating system file descriptor. For example, if you open a pair of pipes, a single IO object contains both a read pipe and a write pipe.] There may be more to an IO object than meets the eye, but in the end you still simply write to it and read from it. In this chapter, we'll be concentrating on class IO and its most commonly used subclass, class File. For more details on using the socket classes for networking, see the section beginning on page 473.
Opening and Closing Files As you might expect, you can create a new file object using File::new.
http://www.rubycentral.com/book/tut_io.html (1 of 6) [7/20/2001 9:49:11 PM]
Programming Ruby: The Pragmatic Programmer's Guide
aFile = File.new("testfile", "r") # ... process the file aFile.close You can create a File object that is open for reading, writing, or both, according to the mode string (here we opened ``testfile'' for reading with an ``r''). The full list of allowed modes appears on page 331. You can also optionally specify file permissions when creating a file; see the description of File::new on page 308 for details. After opening the file, we can work with it, writing and/or reading data as needed. Finally, as responsible software citizens, we close the file, ensuring that all buffered data is written and that all related resources are freed. But here Ruby can make life a little bit easier for you. The method File::open also opens a file. In regular use, it behaves just like File::new. However, if there's a block associated with the call, open behaves differently. Instead of returning a new File object, it invokes the block, passing the newly opened File as a parameter. When the block exits, the file is automatically closed. File.open("testfile", "r") do |aFile| # ... process the file end
Reading and Writing Files The same methods that we've been using for ``simple'' I/O are available for all file objects. So, gets reads a line from standard input, and aFile.gets reads a line from the file object aFile. However, I/O objects enjoy an additional set of access methods, all intended to make our lives easier.
Iterators for Reading As well as using the usual loops to read data from an IO stream, you can also use various Ruby iterators. IO#each_byte invokes a block with the next 8-bit byte from the IO object (in this case, an object of type File). aFile = File.new("testfile") aFile.each_byte {|ch| putc ch; putc ?. } produces:
http://www.rubycentral.com/book/tut_io.html (2 of 6) [7/20/2001 9:49:11 PM]
Programming Ruby: The Pragmatic Programmer's Guide
T.h.i.s. .i.s. .l.i.n.e. .o.n.e. .T.h.i.s. .i.s. .l.i.n.e. .t.w.o. .T.h.i.s. .i.s. .l.i.n.e. .t.h.r.e.e. .A.n.d. .s.o. .o.n....... . IO#each_line calls the block with the next line from the file. In the next example, we'll make the original newlines visible using String#dump, so you can see that we're not cheating. aFile.each_line {|line| puts "Got #{line.dump}" } produces: Got Got Got Got
"This is line one\n" "This is line two\n" "This is line three\n" "And so on...\n"
You can pass each_line any sequence of characters as a line separator, and it will break up the input accordingly, returning the line ending at the end of each line of data. That's why you see the ``\n'' characters in the output of the previous example. In the next example, we'll use ``e'' as the line separator. aFile.each_line("e") do |line| puts "Got #{ line.dump }" end produces: Got Got Got Got Got Got Got
"This is line" " one" "\nThis is line" " two\nThis is line" " thre" "e" "\nAnd so on...\n"
If you combine the idea of an iterator with the auto-closing block feature, you get IO::foreach. This method takes the name of an I/O source, opens it for reading, calls the iterator once for every line in the file, and then closes the file automatically. IO.foreach("testfile") { |line| puts line } produces:
http://www.rubycentral.com/book/tut_io.html (3 of 6) [7/20/2001 9:49:11 PM]
Programming Ruby: The Pragmatic Programmer's Guide
This is line one This is line two This is line three And so on... Or, if you prefer, you can retrieve an entire file into an array of lines: arr = IO.readlines("testfile") » 4 arr.length » "This is line one\n" arr[0] Don't forget that I/O is never certain in an uncertain world---exceptions will be raised on most errors, and you should be ready to catch them and take appropriate action.
Writing to Files So far, we've been merrily calling puts and print, passing in any old object and trusting that Ruby will do the right thing (which, of course, it does). But what exactly is it doing? The answer is pretty simple. With a couple of exceptions, every object you pass to puts and print is converted to a string by calling that object's to_s method. If for some reason the to_s method doesn't return a valid string, a string is created containing the object's class name and id, something like . The exceptions are simple, too. The nil object will print as the string ``nil,'' and an array passed to puts will be written as if each of its elements in turn were passed separately to puts. What if you want to write binary data and don't want Ruby messing with it? Well, normally you can simply use IO#print and pass in a string containing the bytes to be written. However, you can get at the low-level input and output routines if you really want---have a look at the documentation for IO#sysread and IO#syswrite on page 339. And how do you get the binary data into a string in the first place? The two common ways are to poke it in byte by byte or to use Array#pack. str = "" str output.txt") if fork == nil # The sort is now running in a child process # carry on processing in the main program # then wait for the sort to finish Process.wait The call to Kernel::fork returns a process id in the parent, and nil in the child, so the child process will perform the Kernel::exec call and run sort. Sometime later, we issue a Process::wait call, which waits for the sort to complete (and returns its process id). If you'd rather be notified when a child exits (instead of just waiting around), you can set up a signal handler using Kernel::trap (described on page 431). Here we set up a trap on SIGCLD, which is the signal sent on ``death of child process.''
http://www.rubycentral.com/book/tut_threads.html (4 of 5) [7/20/2001 9:49:12 PM]
Programming Ruby: The Pragmatic Programmer's Guide trap("CLD") { pid = Process.wait puts "Child pid #{pid}: terminated" exit } exec("sort testfile > output.txt") if fork == nil # do other stuff... produces: Child pid 14481: terminated
Blocks and Subprocesses IO::popen works with a block in pretty much the same way as File::open does. Pass popen a command, such as date, and the block will be passed an IO object as a parameter. IO.popen ("date") { |f| puts "Date is #{f.gets}" } produces: Date is Sun Mar
4 23:24:12 CST 2001
The IO object will be closed automatically when the code block exits, just as it is with File::open. If you associate a block with Kernel::fork, the code in the block will be run in a Ruby subprocess, and the parent will continue after the block. fork do puts "In child, pid = #$$" exit 99 end pid = Process.wait puts "Child terminated, pid = #{pid}, exit code = #{$? >> 8}" produces: In child, pid = 14488 Child terminated, pid = 14488, exit code = 99 One last thing. Why do we shift the exit code in $? 8 bits to the right before displaying it? This is a ``feature'' of Posix systems: the bottom 8 bits of an exit code contain the reason the program terminated, while the higher-order 8 bits hold the actual exit code. Previous < Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide" Copyright © 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0. This reference is available for download.
http://www.rubycentral.com/book/tut_threads.html (5 of 5) [7/20/2001 9:49:12 PM]
Contents ^
Next >
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
When Trouble Strikes Sad to say, it is possible to write buggy programs using Ruby. Sorry about that. But not to worry! Ruby has several features that will help debug your programs. We'll look at these features, and then we'll show some common mistakes you can make in Ruby and how to fix them.
Ruby Debugger Ruby comes with a debugger, which is conveniently built into the base system. You can run the debugger by invoking the interpreter with the -r debug option, along with any other Ruby options and the name of your script: ruby -r debug [options] [programfile] [arguments] The debugger supports the usual range of features you'd expect, including the ability to set breakpoints, to step into and step over method calls, and to display stack frames and variables. It can also list the instance methods defined for a particular object or class, and allows you to list and control separate threads within Ruby. Table 12.1 on page 133 lists all of the commands that are available under the debugger. If your Ruby has readline support enabled, you can use cursor keys to move back and forth in command history and use line editing commands to amend previous input. To give you an idea of what the Ruby debugger is like, here is a sample session.
http://www.rubycentral.com/book/trouble.html (1 of 8) [7/20/2001 9:49:13 PM]
Programming Ruby: The Pragmatic Programmer's Guide
% ruby -rdebug t.rb Debug.rb Emacs support available. t.rb:1:def fact(n) (rdb:1) list 1-9 [1, 10] in t.rb => 1 def fact(n) 2 if n irb(main):002:0* irb(main):003:0* 2 irb(main):004:0> 4 irb(main):005:0> irb(main):006:1> irb(main):007:1> nil irb(main):008:0> Hello, world! nil irb(main):009:0>
a = 1 + 2 * 3 / 4 % 5 2+2 def test puts "Hello, world!" end test
irb also allows you to create subsessions, each one of which may have its own context. For example, you can create a subsession with the same (top-level) context as the original session, or create a subsession in the context of a particular class or instance. The sample session shown in Figure 12.1 on page 128 is a bit longer, but shows how you can create subsessions and switch between them. Figure not available... For a full description of all the commands that irb supports, see the reference beginning on page 523. As with the debugger, if your version of Ruby was built with GNU Readline support, you can use arrow keys (as with Emacs) or vi-style key bindings to edit individual lines or to go back and reexecute or edit a previous line---just like a command shell. irb is a great learning tool: it's very handy if you want to try out an idea quickly and see if it works.
Editor Support Ruby is designed to read a program in one pass; this means you can pipe an entire program to Ruby's standard input and it will work just fine. We can take advantage of this feature to run Ruby code from inside an editor. In Emacs, for instance, you can select a region of Ruby text and use the command Meta-| to execute Ruby. The Ruby interpreter will use the selected region as standard input and output will go to a buffer named ``*Shell Command Output*.'' This feature has come in quite handy for us while writing this book---just select a few lines of Ruby in the middle of a paragraph and try it out! You can do something similar in the vi editor using ``:!ruby'' which replaces the program text with its output, or ``:w !ruby'', which displays the output without affecting the buffer. Other editors have similar features. While we are on the subject, this would probably be a good place to mention that there is a Ruby mode for
http://www.rubycentral.com/book/trouble.html (3 of 8) [7/20/2001 9:49:13 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Emacs included in the distribution as misc/ruby-mode.el. There are also several syntax-highlighting modules for vim (an enhanced version of the vi editor), jed, and other editors available on the net as well. Check the Ruby FAQ for current locations and availability.
But It Doesn't Work! So you've read through enough of the book, you start to write your very own Ruby program, and it doesn't work. Here's a list of common gotchas and other tips. ● Attribute setter not being called. Within an object, Ruby will parse setter= as an assignment to a local variable, not as a method call. Use self.setter= to indicate the method call. ● A parse error at the last line of the source often indicates a missing end keyword. ● Make sure that the type of the object you are using is what you think it is. If in doubt, use Object#type to check the type of an object. ●
●
●
● ●
●
●
Make sure that your methods start with a lowercase letter and that classes and constants start with an uppercase letter. If you happen to forget a ``,'' in an argument list---especially to print---you can produce some very odd error messages. Block parameters are actually local variables. If an existing local of the same name exists when the block executes, that variable will be modified by the call to the block. This may or may not be a good thing. Watch out for precedence, especially when using {} instead of do/end. Make sure that the open parenthesis of a method's parameter list butts up against the end of the method name with no intervening spaces. Output written to a terminal may be buffered. This means that you may not see a message you write immediately. In addition, if you write messages to both $stdout and $stderr, the output may not appear in the order you were expecting. Always use nonbuffered I/O (set sync=true) for debug messages. If numbers don't come out right, perhaps they're strings. Text read from a file will be a String, and will not be automatically converted to a number by Ruby. A call to to_i will work wonders. A common mistake Perl programmers make is: while gets num1, num2 = split /,/ # ... end
●
Unintended aliasing---if you are using an object as the key of a hash, make sure it doesn't change its hash value (or arrange to call Hash#rehash if it does).
●
Use trace_var to watch when a variable changes value. Use the debugger. Use Object#freeze. If you suspect that some unknown portion of code is setting a variable to a bogus value, try freezing the variable. The culprit will then be caught during the attempt to modify the variable.
● ●
http://www.rubycentral.com/book/trouble.html (4 of 8) [7/20/2001 9:49:13 PM]
Programming Ruby: The Pragmatic Programmer's Guide
There's one major technique that makes writing Ruby code both easier and more fun. Develop your applications incrementally. Write a few lines of code, then run them. Write a few more, then run those. One of the major benefits of an untyped language is that things don't have to be complete before you use them.
But It's Too Slow! Ruby is an interpreted, high-level language, and as such it may not perform as fast as a lower-level language such as C. In this section, we'll list some basic things you can do to improve performance; also have a look in the index under Performance for other pointers.
Create Locals Outside Blocks Try defining the variables used in a block before the block executes. When iterating over a very large set of elements, you can improve execution speed somewhat by predeclaring any iterator variables. In the first example below, Ruby has to create new x and y variables on each iteration, but in the second version it doesn't. We'll use the benchmark package from the Ruby Application Archive to compare the loops: require "benchmark" include Benchmark n = 1000000 bm(12) do |test| test.report("normal:") do n.times do |x| y = x + 1 end end test.report("predefine:") do x = y = 0 n.times do |x| y = x + 1 end end end produces:
normal: predefine:
user 2.510000 2.220000
system 0.000000 0.010000
total 2.510000 ( 2.230000 (
real 2.390549) 2.157877)
Use the Profiler Ruby comes with a code profiler (documentation begins on on page 458). In and of itself, that isn't too surprising, but when you realize that the profiler is written in just about 50 lines of Ruby, that makes for a pretty impressive language. You can add profiling to your code using the command-line option -r profile, or from within the code
http://www.rubycentral.com/book/trouble.html (5 of 8) [7/20/2001 9:49:13 PM]
Programming Ruby: The Pragmatic Programmer's Guide
using require "profile". For example: require "profile" class Peter def initialize(amt) @value = amt end def rob(amt) @value -= amt amt end end class Paul def initialize @value = 0 end def pay(amt) @value += amt amt end end peter = Peter.new(1000) paul = Paul.new 1000.times do paul.pay(peter.rob(10)) end Run this, and you'll get something like the following. time 40.22 25.00 25.00 7.61 2.17 0.00 0.00 0.00 0.00 0.00 0.00
seconds 0.37 0.60 0.83 0.90 0.92 0.92 0.92 0.92 0.92 0.92 0.92
seconds 0.37 0.23 0.23 0.07 0.02 0.00 0.00 0.00 0.00 0.00 0.00
calls 1 1000 1000 1000 1000 1 2 1 4 2 1
ms/call 370.00 0.23 0.23 0.07 0.02 0.00 0.00 0.00 0.00 0.00 0.00
ms/call 920.00 0.25 0.30 0.07 0.02 0.00 0.00 0.00 0.00 0.00 920.00
name Fixnum#times Paul#pay Peter#rob Fixnum#Fixnum#+ Peter#initialize Class#inherited Paul#initialize Module#method_added Class#new #toplevel
With the profiler, you can quickly identify and fix bottlenecks. Remember to check the code without the http://www.rubycentral.com/book/trouble.html (6 of 8) [7/20/2001 9:49:13 PM]
Programming Ruby: The Pragmatic Programmer's Guide
profiler afterward, though---sometimes the slowdown the profiler introduces can mask other problems. Ruby is a wonderfully transparent and expressive language, but it does not relieve the programmer of the need to apply common sense: creating unnecessary objects, performing unneeded work, and creating generally bloated code are wasteful in any language. Debugger commands b[reak] [file:]line b[reak] [file:]name b[reak] wat[ch] expr del[ete] [nnn] disp[lay] expr disp[lay] undisp[lay] [nnn] c[ont] s[tep] nnn=1 n[ext] nnn=1 fi[nish] q[uit] w[here] f[rame] l[ist] [start--end] up nnn=1 down nnn=1 v[ar] g[lobal] v[ar] l[ocal] v[ar] i[stance] obj v[ar] c[onst] Name m[ethod] i[nstance] obj m[ethod] Name th[read] l[ist] th[read] [c[ur[rent]]] th[read] [c[ur[rent]]] nnn th[read] stop nnn th[read] resume nnn [p] expr empty
Set breakpoint at given line in file (default current file). Set breakpoint at method in file. Display breakpoints and watchpoints. Break when expression becomes true. Delete breakpoint nnn (default all). Display value of nnn every time debugger gets control. Show current displays. Remove display (default all). Continue execution. Execute next nnn lines, stepping into methods. Execute next nnn lines, stepping over methods. Finish execution of the current function. Exit the debugger. Display current stack frame. Synonym for where. List source lines from start to end. Move up nnn levels in the stack frame. Move down nnn levels in the stack frame. Display global variables. Display local variables. Display instance variables of obj. Display constants in class or module name. Display instance methods of obj. Display instance methods of the class or module name. List all threads. Display status of current thread. Make thread nnn current and stop it. Make thread nnn current and stop it. Resume thread nnn. Evaluate expr in the current context. expr may include assignment to variables and method invocations. A null command repeats the last command.
http://www.rubycentral.com/book/trouble.html (7 of 8) [7/20/2001 9:49:13 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: abort_on_exception abort_on_exception= critical critical= current exit fork kill list main new pass start stop [ ] [ ]= abort_on_exception abort_on_exception= alive? exit join key? kill priority priority= raise run safe_level status stop? value wakeup Thread encapsulates the behavior of a thread of execution, including the main thread of the Ruby script. See the tutorial in Chapter 11, beginning on page 113. In the descriptions that follow, the parameter aSymbol refers to a symbol, which is either a quoted string or a Symbol (such as :name).
class methods abort_on_exception
Thread.abort_on_exception -> true or false Returns the status of the global ``abort on exception'' condition. The default is false. When set to true, will cause all threads to abort (the process will exit(0)) if an exception is raised in any thread. See also Thread::abort_on_exception=.
abort_on_exception=
Thread.abort_on_exception= aBoolean-> true or false When set to true, all threads will abort if an exception is raised. Returns the new state. Thread.abort_on_exception = true t1 = Thread.new do puts "In second thread" raise "Raise exception" end t1.join print "not reached\n" produces: In second thread prog.rb:4: Raise exception (RuntimeError) from prog.rb:2:in `initialize' from prog.rb:2:in `new' from prog.rb:2
http://www.rubycentral.com/book/ref_c_thread.html (1 of 8) [7/20/2001 9:49:14 PM]
Programming Ruby: The Pragmatic Programmer's Guide
critical
Thread.critical -> true or false Returns the status of the global ``thread critical'' condition.
critical=
Thread.critical= aBoolean -> true or false Sets the status of the global ``thread critical'' condition and returns it. When set to true, prohibits scheduling of any existing thread. Does not block new threads from being created and run. Certain thread operations (such as stopping or killing a thread, sleeping in the current thread, and raising an exception) may cause a thread to be scheduled even when in a critical section. count=0 Thread.new { while true; sleep(1); print "a "; count+=1; end } while count < 3 do end # no-op wait Thread.critical = true puts "no more a's will come out." produces: a a a no more a's will come out.
current
Thread.current -> aThread Returns the currently executing thread. Thread.current
exit
» #
Thread.exit Terminates the currently running thread and schedules another thread to be run. If this thread is already marked to be killed, exit returns the Thread. If this is the main thread, or the last thread, exit the process.
fork
Thread.fork { block } -> aThread Synonym for Thread::new.
kill
Thread.kill( aThread ) Causes the given thread to exit (see Thread::exit). count = 0 a = Thread.new { while true do count += 1 end } » 1 sleep(1) » # Thread.kill(a) » 911276 count » false a.alive?
list
Thread.list -> anArray
http://www.rubycentral.com/book/ref_c_thread.html (2 of 8) [7/20/2001 9:49:14 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns an array of Thread objects for all threads that are either runnable or stopped. Thread.new { sleep(200) } Thread.new { 1000000.times {|i| i*i } } Thread.new { Thread.stop } l = Thread.list l » [#, #, #, #]
main
Thread.main -> aThread Returns the main thread for the process. Thread.main
new
» #
Thread.new( [arg]* ) {| args | block } -> aThread Creates a new thread to execute the instructions given in block, and begins running it. Any arguments passed to Thread::new are passed into the block. x = Thread.new { sleep .1; print "x"; print "y"; print "z" } a = Thread.new { print "a"; print "b"; sleep .2; print "c" } x.join # Let the threads finish before a.join # main thread exits... produces: abxyzc
pass
Thread.pass Invokes the thread scheduler to pass execution to another thread. a = Thread.new { print print print b = Thread.new { print print print a.join b.join
"a"; Thread.pass; "b"; Thread.pass; "c" } "x"; Thread.pass; "y"; Thread.pass; "z" }
produces: axbycz
start
Thread.start( [args]* ) {| args | block } -> aThread
http://www.rubycentral.com/book/ref_c_thread.html (3 of 8) [7/20/2001 9:49:14 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Basically the same as Thread::new. However, if class Thread is subclassed, then calling start in that subclass will not invoke the subclass's initialize method.
stop
Thread.stop Stops execution of the current thread, putting it into a ``sleep'' state, and schedules execution of another thread. Resets the ``critical'' condition to false. a = Thread.new { print "a"; Thread.stop; print "c" } Thread.pass print "b" a.run a.join produces: abc
instance methods []
thr[ aSymbol ] -> anObject or nil Attribute Reference---Returns the value of a thread-local variable, using either a symbol or a string name. If the specified variable does not exist, returns nil. a = Thread.new { b = Thread.new { c = Thread.new { Thread.list.each
Thread.current["name"] = "A"; Thread.stop } Thread.current[:name] = "B"; Thread.stop } Thread.current["name"] = "C"; Thread.stop } {|x| print x.inspect, x[:name], "\n" }
produces: #nil
thr[ aSymbol ] = anObject-> anObject Attribute Assignment---Sets or creates the value of a thread-local variable, using either a symbol or a string. See also Thread#[].
abort_on_exception
thr.abort_on_exception -> true or false Returns the status of the ``abort on exception'' condition for thr. The default is false. See also Thread::abort_on_exception=.
http://www.rubycentral.com/book/ref_c_thread.html (4 of 8) [7/20/2001 9:49:14 PM]
Programming Ruby: The Pragmatic Programmer's Guide
abort_on_exception=
thr.abort_on_exception= true or false-> true or false
When set to true, causes all threads (including the main program) to abort if an exception is raised in thr. The process will effectively exit(0).
alive?
thr.alive? -> true or false Returns true if thr is running or sleeping. »
Thread.current.alive?
exit
true
thr.exit -> thr or nil Terminates thr and schedules another thread to be run. If this thread is already marked to be killed, exit returns the Thread. If this is the main thread, or the last thread, exits the process.
join
thr.join -> thr The calling thread will suspend execution and run thr. Does not return until thr exits. Any threads not joined will be killed when the main program exits. a = Thread.new { print "a"; sleep(10); print "b"; print "c" } x = Thread.new { print "x"; Thread.pass; print "y"; print "z" } x.join # Let x thread finish, a will be killed on exit. produces: axyz
key?
thr.key?( aSymbol ) -> true or false Returns true if the given string (or symbol) exists as a thread-local variable. me = Thread.current me[:oliver] = "a" me.key?(:oliver) me.key?(:stanley)
kill
thr.kill Synonym for Thread#exit.
priority
thr.priority -> anInteger
http://www.rubycentral.com/book/ref_c_thread.html (5 of 8) [7/20/2001 9:49:14 PM]
» »
true false
Programming Ruby: The Pragmatic Programmer's Guide
Returns the priority of thr. Default is zero; higher-priority threads will run before lower-priority threads. »
Thread.current.priority
priority=
0
thr.priority= anInteger -> thr Sets the priority of thr to anInteger. Higher-priority threads will run before lower-priority threads. count1 = count2 = 0 a = Thread.new do loop { count1 += 1 } end a.priority = -1 b = Thread.new do loop { count2 += 1 } end b.priority = -2 sleep 1 Thread.critical = 1 count1 count2
raise
»
1
» »
619920 11586
thr.raise( anException ) Raises an exception (see Kernel::raise on page 425 for details) from thr. The caller does not have to be thr. Thread.abort_on_exception = true a = Thread.new { sleep(200) } a.raise("Gotcha") produces: prog.rb:3: Gotcha (RuntimeError) from prog.rb:2:in `initialize' from prog.rb:2:in `new' from prog.rb:2
run
thr.run -> thr
http://www.rubycentral.com/book/ref_c_thread.html (6 of 8) [7/20/2001 9:49:14 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Wakes up thr, making it eligible for scheduling. If not in a critical section, then invokes the scheduler. a = Thread.new { puts "a"; Thread.stop; puts "c" } Thread.pass puts "Got here" a.run a.join produces: a Got here c
safe_level
thr.safe_level -> anInteger Returns the safe level in effect for thr. » 0
Thread.current.safe_level
status
thr.status -> aString, false or nil Returns the status of thr: ``sleep'' if thr is sleeping or waiting on I/O, ``run'' if thr is executing, false if thr terminated normally, and nil if thr terminated with an exception. a = Thread.new { raise("die now") } b = Thread.new { Thread.stop } c = Thread.new { Thread.exit } a.status b.status c.status Thread.current.status
stop?
» » » »
nil "sleep" false "run"
thr.stop? -> true or false Returns true if thr is dead or sleeping. a = Thread.new { Thread.stop } b = Thread.current a.stop? b.stop?
value
» »
true false
thr.value -> anObject Waits for thr to complete (via Thread#join) and returns its value. a = Thread.new { 2+2 } a.value
http://www.rubycentral.com/book/ref_c_thread.html (7 of 8) [7/20/2001 9:49:14 PM]
»
4
Programming Ruby: The Pragmatic Programmer's Guide
wakeup
thr.wakeup -> thr Marks thr as eligible for scheduling (it may still remain blocked on I/O, however). Does not invoke the scheduler (see Thread#run). c = Thread.new { Thread.stop; puts "hey!" } c.wakeup produces: hey!
Previous < Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide" Copyright © 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0. This reference is available for download.
http://www.rubycentral.com/book/ref_c_thread.html (8 of 8) [7/20/2001 9:49:14 PM]
Contents ^
Next >
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
aFixnum Returns the effective group id for this process. Process.egid
egid=
Process.egid= aFixnum -> aFixnum Sets the effective group id for this process.
http://www.rubycentral.com/book/ref_m_process.html (1 of 5) [7/20/2001 9:49:15 PM]
»
500
Programming Ruby: The Pragmatic Programmer's Guide
euid
Process.euid -> aFixnum Returns the effective user id for this process. Process.euid
euid=
»
501
Process.euid= aFixnum Sets the effective user id for this process. Not available on all platforms.
exit!
Process.exit!( aFixnum=-1 ) Exits the process immediately. No exit handlers are run. aFixnum is returned to the underlying system as the exit status. Process.exit!(0)
fork
Process.fork [{ block } ] -> aFixnum or nil See Kernel::fork on page 420.
getpgid
Process.getpgid( anInteger ) -> anInteger Returns the process group id for the given process id. Not available on all platforms. » 13790
Process.getpgid(Process.ppid())
getpgrp
Process.getpgrp -> anInteger Returns the process group id for this process. Not available on all platforms. Process.getpgid(0) Process.getpgrp
getpriority
» »
13790 13790
Process.getpriority( aKind, anInteger ) -> aFixnum Gets the scheduling priority for specified process, process group, or user. aKind indicates the kind of entity to find: one of Process::PRIO_PGRP, Process::PRIO_USER, or Process::PRIO_PROCESS. anInteger is an id indicating the particular process, process group, or user (an id of 0 means current). Lower priorities are more favorable for scheduling. Not available on all platforms. Process.getpriority(Process::PRIO_USER, 0) Process.getpriority(Process::PRIO_PROCESS, 0)
http://www.rubycentral.com/book/ref_m_process.html (2 of 5) [7/20/2001 9:49:15 PM]
» 0 » 19
Programming Ruby: The Pragmatic Programmer's Guide
gid
Process.gid -> aFixnum Returns the group id for this process. »
Process.gid
gid=
500
Process.gid= aFixnum -> aFixnum Sets the group id for this process.
kill
Process.kill( aSignal, [aPid]+ ) -> aFixnum Sends the given signal to the specified process id(s), or to the current process if aPid is zero. aSignal may be an integer signal number or a POSIX signal name (either with or without a SIG prefix). If aSignal is negative (or starts with a ``-'' sign), kills process groups instead of processes. Not all signals are available on all platforms. trap("SIGHUP") { close_then_exit } Process.kill("SIGHUP", 0)
pid
Process.pid -> aFixnum Returns the process id of this process. Not available on all platforms. Process.pid
ppid
»
16488
Process.ppid -> aFixnum Returns the process id of the parent of this process. Always returns 0 on NT. Not available on all platforms. print "I am ", Process.pid, "\n" Process.fork { print "Dad is ", Process.ppid, "\n" } produces: I am 16490 Dad is 16490
setpgid
Process.setpgid( aPid, anInteger ) -> 0 Sets the process group id of aPid (0 indicates this process) to anInteger. Not available on all platforms.
http://www.rubycentral.com/book/ref_m_process.html (3 of 5) [7/20/2001 9:49:15 PM]
Programming Ruby: The Pragmatic Programmer's Guide
setpgrp
Process.setpgrp -> 0 Equivalent to setpgid(0,0). Not available on all platforms.
setpriority
Process.setpriority( kind, anInteger, anIntPriority ) -> 0 See Process#getpriority. Process.setpriority(Process::PRIO_USER, 0, 19) Process.setpriority(Process::PRIO_PROCESS, 0, 19) Process.getpriority(Process::PRIO_USER, 0) Process.getpriority(Process::PRIO_PROCESS, 0)
setsid
» » » »
0 0 19 19
Process.setsid -> aFixnum Establishes this process as a new session and process group leader, with no controlling tty. Returns the session id. Not available on all platforms. Process.setsid
uid
»
16495
Process.uid -> aFixnum Returns the user id of this process. Process.uid
uid=
»
501
Process.uid= anInteger -> aNumeric Sets the (integer) user id for this process. Not available on all platforms.
wait
Process.wait -> aFixnum Waits for any child process to exit and returns the process id of that child. Raises a SystemError if there are no child processes. Not available on all platforms. Process.fork { exit 1; } Process.wait
wait2
» 16500 » 16500
Process.wait2 -> anArray Waits for any child process to exit and returns an array containing the process id and the exit status of that child. Raises a SystemError if there are no child processes. Process.fork { exit 1 } Process.wait2
http://www.rubycentral.com/book/ref_m_process.html (4 of 5) [7/20/2001 9:49:15 PM]
» 16503 » [16503, 256]
Programming Ruby: The Pragmatic Programmer's Guide
waitpid
Process.waitpid( aPid, anInteger=0 ) -> aPid Waits for the given child process to exit. anInteger may be a logical or of the flag value Process::WNOHANG (do not block if no child available) or Process::WUNTRACED (return stopped children that haven't been reported). Not all flags are available on all platforms, but a flag value of zero will work on all platforms. include Process pid = fork { sleep 3 } Time.now waitpid(pid, Process::WNOHANG) Time.now waitpid(pid, 0) Time.now
waitpid2
» » » » » »
16506 Sun Mar 04 23:31:14 CST 2001 nil Sun Mar 04 23:31:14 CST 2001 16506 Sun Mar 04 23:31:17 CST 2001
Process.waitpid2( aPid, anInteger=0 ) -> anArray Waits for the given child process to exit, returning that child's process id and exit status. anInteger may be a logical or of the flag value Process::WNOHANG (do not block if no child available) or Process::WUNTRACED (return stopped children that haven't been reported). Not all flags are available on all platforms, but a flag value of zero will work on all platforms.
Previous
[cookie] ){ cgi.html{ "\nHTML content here" } } produces: Content-Type: text/html Content-Length: 86 Set-Cookie: rubyweb=CustID%3D123&Part%3DABC; path= HTML content here The next time the user comes back to this page, you can retrieve the cookie values for CustID and Part, as shown in the HTML output. require "cgi" cgi = CGI.new("html3") cgi.out{ cgi.html{ cgi.pre{ cookie = cgi.cookies["rubyweb"] "\nCookies are\n" + cookie.value.join("\n") } } } produces:
http://www.rubycentral.com/book/web.html (4 of 9) [7/20/2001 9:49:18 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Content-Type: text/html Content-Length: 111 Cookies are CustID=123 Part=ABC
Sessions Cookies by themselves still need a bit of work to be useful. What we really want is a session: a persistent state for some Web surfer. Sessions are handled with CGI::Session (documented beginning on page 508), which uses cookies but provides a higher-level abstraction.
require "cgi" require "cgi/session" cgi = CGI.new("html3") sess = CGI::Session.new( cgi, "session_key" => "rubyweb", "session_id" => "9650", "new_session" => true, "prefix" => "web-session.") sess["CustID"] = 123 sess["Part"] = "ABC" cgi.out{ cgi.html{ "\nHTML content here" } } This will send a cookie to the user named ``rubyweb'' with a value of 9650. It will also create a disk file in $TMP/web-session.9650 with the key, value pairs for CustID and Part. When the user returns, all you need is a parameter to indicate the session id. In this example, that would be rubyweb=9650. With that value in the parameters, you'll be able to retrieve the full set of saved session data.
http://www.rubycentral.com/book/web.html (5 of 9) [7/20/2001 9:49:18 PM]
Programming Ruby: The Pragmatic Programmer's Guide
require "cgi" require "cgi/session" cgi = CGI.new("html3") sess = CGI::Session.new( cgi, "session_key" => "rubyweb", "prefix" => "web-session.") cgi.out{ cgi.html{ "\nCustomer #{sess['CustID']} orders an #{sess['Part']}" } }
Embedding Ruby in HTML So far we've looked at using Ruby to create HTML output, but we can turn the problem inside out; we can actually embed Ruby in an HTML document. There are a number of packages that allow you to embed Ruby statements in some other sort of a document, especially in an HTML page. Generically, this is known as ``eRuby.'' Specifically, there are several different implementations of eRuby, including eruby and erb. The remainder of this section will discuss eruby, written by Shugo Maeda. Embedding Ruby in HTML is a very powerful concept---it basically gives us the equivalent of a tool such as ASP, JSP, or PHP, but with the full power of Ruby.
Using eruby eruby acts as a filter, plain and simple. Any text within the input file is passed through untouched, with the following exceptions: Expression
Description The Ruby code between the delimiters is replaced with its output. The Ruby expression between the delimiters is replaced with its value. The Ruby code between the delimiters is ignored (useful for testing).
You invoke eruby as: eruby [options] [document] If the document is omitted, eruby will read from standard input. The command-line options for eruby are shown in Table 14.1 on page 151.
http://www.rubycentral.com/book/web.html (6 of 9) [7/20/2001 9:49:18 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Command-line options for eruby Option -d, --debug -Kkcode -Mmode
Description Sets $DEBUG to true. Specifies an alternate coding system (see page 139). Specifies runtime mode, one of: f filter mode c CGI mode (prints errors as HTML, sets $SAFE=1) n NPH-CGI mode (prints extra HTTP headers, sets $SAFE=1)
-n, --noheader Disables CGI header output. -v, --verbose Enables verbose mode. Prints version information and exits. --version Let's look at some simple examples. We'll run the eruby executable on the following input. This text is eruby substitutes the expression between the delimiters and produces This text is 100% Live! Using the 'left', 'padx'=>10, 'pady'=>10) } We can also bind a Ruby variable to a Tk widget's value using a TkVariable proxy. We show this in the following example. Notice how the TkCheckButton is set up; the documentation says that the variable option takes a var reference as an argument. For this, we create a Tk variable reference using TkVariable#new. Accessing mycheck.value will return the string ``0'' or ``1'' depending on whether the checkbox is checked. You can use the same mechanism for anything that supports a var reference, such as radio buttons and text fields. mycheck = TkVariable.new TkCheckButton.new(top) { variable mycheck pack('padx'=>5, 'pady'=>5, 'side' => 'left') }
Setting/Getting Options Dynamically In addition to setting a widget's options when it's created, you can reconfigure a widget while it's running. Every widget supports the configure method, which takes a Hash or a code block in the same manner as new. We can modify the first example to change the label text in response to a button press: lbl = TkLabel.new(top) { justify 'center' text 'Hello, World!'; pack('padx'=>5, 'pady'=>5, 'side' => 'top') } TkButton.new(top) { text "Cancel" command proc { lbl.configure('text'=>"Goodbye, Cruel World!") } pack('side'=>'right', 'padx'=>10, 'pady'=>10) }
http://www.rubycentral.com/book/ext_tk.html (3 of 11) [7/20/2001 9:49:19 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Now when the Cancel button is pressed, the text in the label will change immediately from ``Hello, World!'' to ``Goodbye, Cruel World!'' You can also query widgets for particular option values using cget: require 'tk' b = TkButton.new { text "OK" justify 'left' border 5 } b.cget('text') b.cget('justify') b.cget('border')
» » »
"OK" "left" 5
Sample Application Here's a slightly longer example, showing a genuine application---a ``pig Latin'' generator. Type in the phrase such as ``Ruby rules,'' and the ``Pig It'' button will instantly translate it into pig Latin. require 'tk' class PigBox def pig(word) leadingCap = word =~ /^A-Z/ word.downcase res = case word when /^aeiouy/ word+"way" when /^([^aeiouy]+)(.*)/ $2+$1+"ay" else word end leadingCap ? res.capitalize : res end def showPig @text.value = @text.value.split.collect{|w| pig(w)}.join(" ") end def initialize ph = { 'padx' => 10, 'pady' => 10 }
http://www.rubycentral.com/book/ext_tk.html (4 of 11) [7/20/2001 9:49:19 PM]
# common options
Programming Ruby: The Pragmatic Programmer's Guide
p = proc {showPig} @text = TkVariable.new root = TkRoot.new { title "Pig" } top = TkFrame.new(root) TkLabel.new(top) {text 'Enter Text:' ; pack(ph) } @entry = TkEntry.new(top, 'textvariable' => @text) @entry.pack(ph) TkButton.new(top) {text 'Pig It'; command p; pack ph} TkButton.new(top) {text 'Exit'; command {proc exit}; pack ph} top.pack('fill'=>'both', 'side' =>'top') end end PigBox.new Tk.mainloop Sidebar: Geometry Management In the example code in this chapter, you'll see references to the widget method pack. That's a very important call, as it turns out---leave it off and you'll never see the widget. pack is a command that tells the geometry manager to place the widget according to constraints that we specify. Geometry managers recognize three commands: Command pack place grid
Placement Specification Flexible, constraint-based placement Absolute position Tabular (row/column) position
As pack is the most commonly used command, we'll use it in our examples.
Binding Events Our widgets are exposed to the real world; they get clicked on, the mouse moves over them, the user tabs into them; all these things, and more, generate events that we can capture. You can create a binding from an event on a particular widget to a block of code, using the widget's bind method. For instance, suppose we've created a button widget that displays an image. We'd like the image to change when the user's mouse is over the button.
http://www.rubycentral.com/book/ext_tk.html (5 of 11) [7/20/2001 9:49:19 PM]
Programming Ruby: The Pragmatic Programmer's Guide
image1 = TkPhotoImage.new { file "img1.gif" } image2 = TkPhotoImage.new { file "img2.gif" } b = TkButton.new(@root) { image image1 command proc { doit } } b.bind("Enter") { b.configure('image'=>image2) } b.bind("Leave") { b.configure('image'=>image1) } First, we create two GIF image objects from files on disk, using TkPhotoImage. Next we create a button (very cleverly named ``b''), which displays the image image1. We then bind the ``Enter'' event so that it dynamically changes the image displayed by the button to image2, and the ``Leave'' event to revert back to image1. This example shows the simple events ``Enter'' and ``Leave.'' But the named event given as an argument to bind can be composed of several substrings, separated with dashes, in the order modifier-modifier-type-detail. Modifiers are listed in the Tk reference and include Button1, Control, Alt, Shift, and so on. Type is the name of the event (taken from the X11 naming conventions) and includes events such as ButtonPress, KeyPress, and Expose. Detail is either a number from 1 to 5 for buttons or a keysym for keyboard input. For instance, a binding that will trigger on mouse release of button 1 while the control key is pressed could be specified as: Control-Button1-ButtonRelease or Control-ButtonRelease-1 The event itself can contain certain fields such as the time of the event and the x and y positions. bind can pass these items to the callback, using event field codes. These are used like printf specifications. For instance, to get the x and y coordinates on a mouse move, you'd specify the call to bind with three parameters. The second parameter is the Proc for the callback, and the third parameter is the event field string. canvas.bind("Motion", proc{|x, y| do_motion (x, y)}, "%x %y")
Canvas Tk provides a Canvas widget with which you can draw and produce PostScript output. Here's a simple bit of code (adapted from the distribution) that will draw straight lines. Clicking and holding button 1 will start a line, which will be ``rubber-banded'' as you move the mouse around. When you release button 1, the line will be drawn in that position. Pressing button 2 on the mouse will dump out a PostScript representation of the drawing canvas, suitable for printing.
http://www.rubycentral.com/book/ext_tk.html (6 of 11) [7/20/2001 9:49:19 PM]
Programming Ruby: The Pragmatic Programmer's Guide
require 'tk' class Draw def do_press(x, y) @start_x = x @start_y = y @current_line = TkcLine.new(@canvas, x, y, x, y) end def do_motion(x, y) if @current_line @current_line.coords @start_x, @start_y, x, y end end def do_release(x, y) if @current_line @current_line.coords @start_x, @start_y, x, y @current_line.fill 'black' @current_line = nil end end def initialize(parent) @canvas = TkCanvas.new(parent) @canvas.pack @start_x = @start_y = 0 @canvas.bind("1", proc{|e| do_press(e.x, e.y)}) @canvas.bind("2", proc{ puts @canvas.postscript({}) }) @canvas.bind("B1-Motion", proc{|x, y| do_motion(x, y)}, "%x %y") @canvas.bind("ButtonRelease-1", proc{|x, y| do_release (x, y)}, "%x %y") end end root = TkRoot.new{ title 'Canvas' } Draw.new(root) Tk.mainloop A few mouse clicks, and you've got an instant masterpiece: Missing screenshots/canvas.ps ``We couldn't find the artist, so we had to hang the picture....''
http://www.rubycentral.com/book/ext_tk.html (7 of 11) [7/20/2001 9:49:19 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Scrolling Unless you plan on drawing very small pictures, the previous example may not be all that useful. TkCanvas, TkListbox, and TkText can be set up to use scrollbars, so you can work on a smaller subset of the ``big picture.'' Communication between a scrollbar and a widget is bidirectional. Moving the scrollbar means that the widget's view has to change; but when the widget's view is changed by some other means, the scrollbar has to change as well to reflect the new position. Since we haven't done much with lists yet, our scrolling example will use a scrolling list of text. In the following code fragment, we'll start off by creating a plain old TkListbox. Then, we'll make a TkScrollbar. The scrollbar's callback (set with command) will call the list widget's yview method, which will change the value of the visible portion of the list in the y-direction. After that callback is set up, we make the inverse association: when the list feels the need to scroll, we'll set the appropriate range in the scrollbar using TkScrollbar#set. We'll use this same fragment in a fully functional program in the next section. list_w = TkListbox.new(frame, 'selectmode' => 'single') scroll_bar = TkScrollbar.new(frame, 'command' => proc { |*args| list_w.yview *args }) scroll_bar.pack('side' => 'left', 'fill' => 'y') list_w.yscrollcommand(proc { |first,last| scroll_bar.set(first,last) })
Just One More Thing We could go on about Tk for another few hundred pages, but that's another book. The following program is our final Tk example---a simple GIF image viewer. You can select a GIF filename from the scrolling list, and a thumb nail version of the image will be displayed. There are just a few more things we'd like to point out. Have you ever seen an application that creates a ``busy cursor'' and then forgets to reset it to normal? There's a neat trick in Ruby that will prevent this from happening. Remember how File::new uses a block to ensure that the file is closed after it is used? We can do a similar thing with the method busy, as shown in the next example. This program also demonstrates some simple TkListbox manipulations---adding elements to the list, setting up a callback on a mouse button release,[You probably want the button release, not the press, as the widget gets selected on the button press.] and retrieving the current selection. So far, we've used TkPhotoImage to only display icons directly, but you can also zoom, subsample, and show portions of images as well. Here we use the subsample feature to scale down the image for viewing. http://www.rubycentral.com/book/ext_tk.html (8 of 11) [7/20/2001 9:49:19 PM]
Programming Ruby: The Pragmatic Programmer's Guide
require 'tk' def busy begin $root.cursor $root.update yield # Call ensure $root.cursor $root.update end end
"watch" # Set a watch cursor # Make sure it updates the screen the associated block "" # Back to original
$root = TkRoot.new {title 'Scroll List'} frame = TkFrame.new($root) list_w = TkListbox.new(frame, 'selectmode' => 'single') scroll_bar = TkScrollbar.new(frame, 'command' => proc { |*args| list_w.yview *args }) scroll_bar.pack('side' => 'left', 'fill' => 'y') list_w.yscrollcommand(proc { |first,last| scroll_bar.set(first,last) }) list_w.pack('side'=>'left') image_w = TkPhotoImage.new TkLabel.new(frame, 'image' => image_w).pack('side'=>'left') frame.pack list_contents = Dir["screenshots/gifs/*.gif"] list_contents.each {|x| list_w.insert('end',x) # Insert each file name into the list } list_w.bind("ButtonRelease-1") { index = list_w.curselection[0] busy { tmp_img = TkPhotoImage.new('file'=> list_contents[index]) scale = tmp_img.height / 100 scale = 1 if scale < 1 image_w.copy(tmp_img, 'subsample' => [scale,scale]) http://www.rubycentral.com/book/ext_tk.html (9 of 11) [7/20/2001 9:49:19 PM]
Programming Ruby: The Pragmatic Programmer's Guide
tmp_img = nil # Be sure to remove it, the GC.start # image may have been large } } Tk.mainloop Finally, a word about garbage collection---we happened to have a few very large GIF files lying about[They were technical documents! Really!] while testing this code. We didn't want to carry these huge images around in memory any longer then necessary, so we set the image reference to nil and call the garbage collector immediately to remove the trash.
Translating from Perl/Tk Documentation That's it, you're on your own now. For the most part, you can easily translate the documentation given for Perl/Tk to Ruby. But there are a few exceptions; some methods are not implemented, and there is undocumented extra functionality. Until a Ruby/Tk book comes out, your best bet is to ask on the newsgroup or read the source code. But in general, it's pretty easy to see what's going on. Remember that options may be given as a hash, or in code block style, and the scope of the code block is within the TkWidget being used, not your class instance.
Object Creation Perl/Tk: Ruby:
$widget = $parent->Widget( [ option => value ] ) widget = TkWidget.new(parent, option-hash) widget = TkWidget.new(parent) { code block }
You may not need to save the returned value of the newly created widget, but it's there if you do. Don't forget to pack a widget (or use one of the other geometry calls), or it won't show up.
Options Perl/Tk: Ruby:
-background => color 'background' => color { background color }
Remember that the code block scope is different.
Variable References
http://www.rubycentral.com/book/ext_tk.html (10 of 11) [7/20/2001 9:49:19 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Perl/Tk: Ruby:
-textvariable => \$variable -textvariable => varRef ref = TkVariable.new 'textvariable' => ref { textvariable ref }
Use TkVariable to attach a Ruby variable to a widget's value. You can then use the value accessors in TkVariable (TkVariable#value and TkVariable#value=) to affect the contents of the widget directly. Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Ruby and Microsoft Windows Ruby is written for POSIX environments, which means that it can take advantage of all of the system calls and libraries that Unix programmers are familiar with. But there are a number of features and extensions designed to make Ruby more useful in a Microsoft Windows environment, too. In this chapter, we'll look at these features and share some secrets to using Ruby effectively under Windows.
Ruby Ports Windows does not provide a POSIX environment by itself, so some sort of emulation library is required in order to provide the necessary functions. There are several ports of Ruby for Windows: the most commonly used one relies on the GNU Win32 environment, and is called the ``cygwin32'' port. The cygwin32 port works well with extension libraries, and is available on the Web as a precompiled binary. Another port, ``mswin32,'' does not rely on cygwin. It is currently available as source code only. The remainder of this chapter will refer to the cygwin32 port.
Running Ruby Under Windows There are two executables provided with the cygwin32 Ruby distribution: ruby.exe and rubyw.exe. ruby.exe is meant to be used at a command prompt (a DOS shell), just as in the Unix version. For applications that read and write to the standard input and output, this is fine. But that also means that anytime you run ruby.exe, you'll get a DOS shell even if you don't want one---Windows will create a new command prompt window and display it while Ruby is running. This might not be appropriate behavior if, for example, you double-click on a Ruby script that uses a graphical interface (such as Tk), or if you are running a Ruby script as a background task, or from inside another program. In these cases, you'll want to use rubyw.exe. It is the same as ruby.exe except that it does not provide standard in, standard out, or standard error, and does not launch a DOS shell when run. You can set a file association[Using View/Options/Filetypes from Explorer.] so that files with the extension ``.rb'' will automatically use rubyw.exe. By doing this, you can double-click on Ruby
http://www.rubycentral.com/book/win32.html (1 of 5) [7/20/2001 9:49:20 PM]
Programming Ruby: The Pragmatic Programmer's Guide
scripts and they will simply run without popping up a DOS shell.
Win32API If you plan on doing Ruby programming that needs to access some Windows 32 API functions directly, or to use the entry points in some other DLLs, we've got good news for you---the Win32API extension. The Win32API module is documented beginning on page 512, but here's a quick peek at how it works. You create a Win32API object that represents a call to a particular DLL entry point by specifying the name of the function, the name of the DLL that contains the function, and the function signature (argument types and return type). The resulting object can then be used to make the function call. Many of the arguments to DLL functions are binary structures of some form. Win32API handles this by using Ruby String objects to pass the binary data back and forth. You will need to pack and unpack these strings as necessary (see the example on page 512).
Windows Automation If groveling around in the low-level Windows API doesn't interest you, Windows automation might---you can use Ruby as a client for Windows Automation thanks to a Ruby extension called WIN32OLE, written by Masaki Suketa. The examples in this section are taken from those provided in the WIN32OLE distribution. Windows automation allows an automation controller (a client) to issue commands and queries against an automation server, such as Microsoft Excel, Word, PowerPoint, and so on. You can execute a method of an automation server by calling a method of the same name from a WIN32OLE object. For instance, you can create a new WIN32OLE client that launches a fresh copy of Internet Explorer and commands it to visit the home page. ie = WIN32OLE.new('InternetExplorer.Application') ie.visible = true ie.gohome Methods that aren't known to WIN32OLE (such as visible or gohome) are passed on to the WIN32OLE#invoke method, which sends the proper commands to the server. The WIN32OLE reference beginning on page 509 describes the class in detail, but we'll go over a few of its features here.
Getting and Setting Properties You can set and get properties from the server using normal Ruby hash notation. For example, to set the Rotation property in an Excel chart, you might write
http://www.rubycentral.com/book/win32.html (2 of 5) [7/20/2001 9:49:20 PM]
Programming Ruby: The Pragmatic Programmer's Guide
excel = WIN32OLE.new("excel.application") excelchart = excel.Charts.Add() ... excelchart['Rotation'] = 45 puts excelchart['Rotation'] An OLE object's parameters are automatically set up as attributes of the WIN32OLE object. This means that you can set a parameter by assigning to an object attribute. excelchart.rotation = 45 r = excelchart.rotation Because these attributes are conventional Ruby accessor methods, attribute names cannot start with a capital letter. In this example, we have to use rotation instead of Rotation.
Named Arguments Other automation client languages such as Visual Basic have the concept of named arguments. Suppose you had a Visual Basic routine with the signature: Song(artist, title, length):
rem Visual Basic
Instead of calling it with all three arguments in the order specified, you could use named arguments. Song title := 'Get It On':
rem Visual Basic
This is equivalent to the call Song(nil, 'Get It On', nil). In Ruby, you can use this feature by passing a hash with the named arguments. Song.new( 'title' => 'Get It On' )
for each Where Visual Basic has a ``for each'' statement to iterate over a collection of items in a server, a WIN32OLE object has an each method (which takes a block) to accomplish the same thing.
An Example The following example, using Microsoft Excel, illustrates most of these concepts. First, we create a new WIN32OLE object attached to Excel and set some cell values. Next we select a range of cells and create a chart. We set the Type property in the excelchart object to make it a 3D chart. Next we'll loop through and change the chart rotation, 10° at a time. We'll add a few charts, and we'll use each to step through and print them out. Finally, we'll close down the Excel application and exit.
http://www.rubycentral.com/book/win32.html (3 of 5) [7/20/2001 9:49:20 PM]
Programming Ruby: The Pragmatic Programmer's Guide
require 'win32ole' # -4100 is the value for the Excel constant xl3DColumn. ChartTypeVal = -4100; # Creates OLE object to Excel excel = WIN32OLE.new("excel.application") # Create and rotate the chart excel['Visible'] = TRUE; workbook = excel.Workbooks.Add(); excel.Range("a1")['Value'] = 3; excel.Range("a2")['Value'] = 2; excel.Range("a3")['Value'] = 1; excel.Range("a1:a3").Select(); excelchart = workbook.Charts.Add(); excelchart['Type'] = ChartTypeVal; 30.step(180, 10) do |rot| excelchart['Rotation'] = rot end excelchart2 = workbook.Charts.Add(); excelchart3 = workbook.Charts.Add(); charts = workbook.Charts charts.each { |i| puts i } excel.ActiveWorkbook.Close(0); excel.Quit();
Optimizing As with most (if not all) high-level languages, it can be all too easy to churn out code that is unbearably slow, but that can be easily fixed with a little thought. With WIN32OLE, you need to be careful with unnecessary dynamic lookups. Where possible, it is better to assign a WIN32OLE object to a variable and then reference elements from it, rather than creating a long chain of ``.'' expressions. For example, instead of writing
http://www.rubycentral.com/book/win32.html (4 of 5) [7/20/2001 9:49:20 PM]
Programming Ruby: The Pragmatic Programmer's Guide
workbook.Worksheets(1).Range("A1").value workbook.Worksheets(1).Range("A2").value workbook.Worksheets(1).Range("A3").value workbook.Worksheets(1).Range("A4").value
= = = =
1 2 4 8
we can eliminate the common subexpressions by saving the first part of the expression to a temporary variable and then make calls from that variable: worksheet = workbook.Worksheets(1) worksheet.Range("A1").value worksheet.Range("A2").value worksheet.Range("A3").value worksheet.Range("A4").value
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Extending Ruby It is easy to extend Ruby with new features by writing code in Ruby. Once you start adding in low-level code written in C, however, the possibilities are endless. Extending Ruby with C is pretty easy. For instance, suppose we are building a custom Internet-ready jukebox for the Sunset Diner and Grill. It will play MP3 audio files from a hard disk or audio CDs from a CD jukebox. We want to be able to control the jukebox hardware from a Ruby program. The hardware vendor gave us a C header file and a binary library to use; our job is to construct a Ruby object that makes the appropriate C function calls. But before we can get Ruby and C to work together, we need to see what the Ruby world looks like from the C side.[Much of the information in this chapter is taken from the README.EXT file that is included in the distribution. If you are planning on writing a Ruby extension, you may want to refer to that file for more details as well as the latest changes.]
Ruby Objects in C The first thing we need to look at is how to represent and access Ruby datatypes from within C. Everything in Ruby is an object, and all variables are references to objects. In C, this means that the type of all Ruby variables is VALUE, which is either a pointer to a Ruby object or an immediate value (such as Fixnum). This is how Ruby implements object-oriented code in C: a Ruby object is an allocated structure in memory that contains a table of instance variables and information about the class. The class itself is another object (an allocated structure in memory) that contains a table of the methods defined for that class. On this foundation hangs all of Ruby.
VALUE as a Pointer When VALUE is a pointer, it is a pointer to one of the defined Ruby object structures---you can't have a VALUE that points to an arbitrary structure. The structures for each built-in class are defined in ``ruby.h'' and are named RClassname, as in RString and RArray. You can check to see what type of structure is used for a particular VALUE in a number of ways. The
http://www.rubycentral.com/book/ext_ruby.html (1 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
macro TYPE(obj) will return a constant representing the C type of the given object: T_OBJECT, T_STRING, and so on. Constants for the built-in classes are defined in ``ruby.h''. Note that the type we are referring to here is an implementation detail---it is not the same as the class of an object. If you want to ensure that a value pointer points to a particular structure, you can use the macro Check_Type, which will raise a TypeError exception if value is not of the expected type (which is one of the constants T_STRING, T_FLOAT, and so on): Check_Type(VALUE value, int type) If speed is an issue, there are faster macros that check specifically for the immediate values Fixnum and nil. FIXNUM_P(value) -> non-zero if value is a Fixnum NIL_P(value) -> non-zero if value is nil RTEST(value) -> non-zero if value is neither nil nor false Again, note that we are talking about ``type'' as the C structure that represents a particular built-in type. The class of an object is a different beast entirely. The class objects for the built-in classes are stored in C global variables named rb_cClassname (for instance, rb_cObject); modules are named rb_mModulename. It wouldn't be advisable to mess with the data in these structures directly, however---you may look, but don't touch unless you are fond of debuggers. You should normally use only the supplied C functions to manipulate Ruby data (we'll talk more about this in just a moment). However, in the interests of efficiency you may need to dig into these structures to obtain data. In order to dereference members of these C structures, you have to cast the generic VALUE to the proper structure type. ruby.h contains a number of macros that perform the proper casting for you, allowing you to dereference structure members easily. These macros are named RCLASSNAME, as in RSTRING or RARRAY. For example: VALUE str, arr; RSTRING(str)->len RSTRING(str)->ptr RARRAY(arr)->len RARRAY(arr)->capa RARRAY(arr)->ptr
-> -> -> -> ->
length of the Ruby string pointer to string storage length of the Ruby array capacity of the Ruby array pointer to array storage
VALUE as an Immediate Object As we said above, immediate values are not pointers: Fixnum, Symbol, true, false, and nil are stored directly in VALUE. Fixnum values are stored as 31-bit numbers[Or 63-bit on wider CPU architectures.] that are formed by shifting the original number left 1 bit and then setting the least significant bit (bit 0) to ``1.'' When VALUE is used as a pointer to a specific Ruby structure, it is guaranteed always to have an LSB of zero; the other immediate values also have LSBs of zero. Thus, a simple bit test can tell you whether or not you have a Fixnum.
http://www.rubycentral.com/book/ext_ruby.html (2 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
There are several useful conversion macros for numbers as well as other standard datatypes shown in Table 17.1 on page 176. The other immediate values (true, false, and nil) are represented in C as the constants Qtrue, Qfalse, and Qnil, respectively. You can test VALUE variables against these constants directly, or use the conversion macros (which perform the proper casting).
Writing Ruby in C One of the joys of Ruby is that you can write Ruby programs almost directly in C. That is, you can use the same methods and the same logic, but with slightly different syntax to accommodate C. For instance, here is a small, fairly boring test class written in Ruby. class Test def initialize @arr = Array.new end def add(anObject) @arr.push(anObject) end end The equivalent code in C should look somewhat familiar. #include "ruby.h" static VALUE t_init(VALUE self) { VALUE arr; arr = rb_ary_new(); rb_iv_set(self, "@arr", arr); return self; } static VALUE t_add(VALUE self, VALUE anObject) { VALUE arr; arr = rb_iv_get(self, "@arr"); rb_ary_push(arr, anObject); return arr; }
http://www.rubycentral.com/book/ext_ruby.html (3 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
VALUE cTest; void Init_Test() { cTest = rb_define_class("Test", rb_cObject); rb_define_method(cTest, "initialize", t_init, 0); rb_define_method(cTest, "add", t_add, 1); } Let's go through this example in detail, as it illustrates many of the important concepts in this chapter. First off, we need to include the header file ``ruby.h'' to obtain the necessary definitions. Now look at the last function, Init_Test. Every class or module defines a C global function named Init_Name. This function will be called when the interpreter first loads the extension Name (or on startup for statically linked extensions). It is used to initialize the extension and to insinuate it into the Ruby environment. In this case, we define a new class named Test, which is a subclass of Object (represented by the external symbol rb_cObject; see ``ruby.h'' for others). Next we set up add and initialize as two instance methods for class Test. The calls to rb_define_method establish a binding between the Ruby method name and the C function that will implement it, so a call to the add method in Ruby will call the C function t_add with one argument. Similarly, when new is called for this class, Ruby will construct a basic object and then call initialize, which we have defined here to call the C function t_init with no (Ruby) arguments. Now go back and look at the definition of initialize. Even though we said it took no arguments, there's a parameter here! In addition to any Ruby arguments, every method is passed an initial VALUE argument that contains the receiver for this method (the equivalent of self in Ruby code). The first thing we'll do in initialize is create a Ruby array and set the instance variable @arr to point to it. Just as you would expect if you were writing Ruby source, referencing an instance variable that doesn't exist creates it. Finally, the function t_add gets the instance variable @arr from the current object and calls Array#push to push the passed value onto that array. When accessing instance variables in this way, the @-prefix is mandatory---otherwise the variable is created, but cannot be referenced from Ruby. Despite the extra, clunky syntax that C imposes, you're still writing in Ruby---you can manipulate objects using all of the method calls you've come to know and love, with the added advantage of being able to craft tight, fast code when needed. WARNING: Every C function that is callable from Ruby must return a VALUE, even if it's just Qnil. Otherwise, a core dump (or GPF) will be the likely result. We can use the C version of the code in Ruby simply by require-ing it dynamically at runtime (on most platforms). require "code/ext/Test" t = Test.new t.add("Bill Chase")
http://www.rubycentral.com/book/ext_ruby.html (4 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
C/Ruby datatype conversion functions and macros C Datatypes to Ruby Objects: INT2NUM(int) INT2FIX(int) INT2NUM(long or int) INT2FIX(long or int) CHR2FIX(char) rb_str_new2(char *) rb_float_new(double) Ruby Objects to C Datatypes: int NUM2INT(Numeric) int FIX2INT(Fixnum) unsigned int NUM2UINT(Numeric) unsigned int FIX2UINT(Fixnum) long NUM2LONG(Numeric) long FIX2LONG(Fixnum) unsigned long NUM2ULONG(Numeric) char NUM2CHR(Numeric or String) char * STR2CSTR(String) char * rb_str2cstr(String, int *length) double NUM2DBL(Numeric)
-> Fixnum or Bignum -> Fixnum (faster) -> Fixnum or Bignum -> Fixnum (faster) -> Fixnum -> String -> Float
(Includes type check) (Faster) (Includes type check) (Includes type check) (Includes type check) (Faster) (Includes type check) (Includes type check) Returns length as well
Evaluating Ruby Expressions in C If you are in the middle of some C code and you want to run an arbitrary Ruby expression without writing a bunch of C, you can always use the C version of eval. Suppose you have a collection of objects that need to have a flag cleared. rb_eval_string("anObject.each{|x| x.clearFlag }"); If you just want to call a particular method (which is cheaper than eval-ing an entire string) you can use rb_funcall(receiver, method_id, argc, ...) Full descriptions of these and other commonly used C functions begin on page 189.
http://www.rubycentral.com/book/ext_ruby.html (5 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Sharing Data Between Ruby and C We've covered enough of the basics now to return to our jukebox example---interfacing C code with Ruby and sharing data and behavior between the two worlds.
Directly Sharing Variables Although you could maintain a C version of some variable along with a separate Ruby version of that variable, and struggle to keep the two in sync,[A clear violation of the DRY--Don't Repeat Yourself---principle described in our book The Pragmatic Programmer .] it would be much better to share a variable directly between Ruby and C. You can share global variables by creating a Ruby object on the C side and then binding its address to a Ruby global variable. In this case, the $ prefix is optional, but it helps clarify that this is a global variable. VALUE hardware_list; hardware_list = rb_ary_new(); rb_define_variable("$hardware", &hardware_list); ... rb_ary_push(hardware_list, rb_str_new2("DVD")); rb_ary_push(hardware_list, rb_str_new2("CDPlayer1")); rb_ary_push(hardware_list, rb_str_new2("CDPlayer2")); The Ruby side can then access the C variable hardware_list as $hardware: $hardware
» ["DVD", "CDPlayer1", "CDPlayer2"]
You can also create hooked variables that will call a specified function when the variable is accessed, and virtual variables that only call the hooks---no actual variable is involved. See the API section that begins on page 192 for details. If you create a Ruby object from C and store it in a C global variable without exporting it to Ruby, you must at least tell the garbage collector about it, lest ye be reaped inadvertently: VALUE obj; obj = rb_ary_new(); rb_global_variable(obj);
Wrapping C Structures Now on to the really fun stuff. We've got the vendor's library that controls the audio CD jukebox units, and we're ready to wire it into Ruby. The vendor's header file looks like this:
http://www.rubycentral.com/book/ext_ruby.html (6 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
typedef struct _cdjb { int statusf; int request; void *data; char pending; int unit_id; void *stats; } CDJukebox; // Allocate a new CDPlayer structure and bring it online CDJukebox *CDPlayerNew(int unit_id); // Deallocate when done (and take offline) void CDPlayerDispose(CDJukebox *rec); // Seek to a disc, track and notify progress void CDPlayerSeek(CDJukebox *rec, int disc, int track, void (*done)(CDJukebox *rec, int percent)); // ... others... // Report a statistic double CDPlayerAvgSeekTime(CDJukebox *rec); This vendor has its act together; while the vendor might not admit it, the code is written with an object-oriented flavor. We don't know what all those fields mean within the CDJukeBox structure, but that's okay---we can treat it as an opaque pile of bits. The vendor's code knows what to do with it, we just have to carry it around. Anytime you have a C-only structure that you would like to handle as a Ruby object, you should wrap it in a special, internal Ruby class called DATA (type T_DATA). There are two macros to do this wrapping, and one to retrieve your structure back out again.
VALUE
VALUE
C Datatype Wrapping Data_Wrap_Struct(VALUE class, void (*mark)(), void (*free)(), void *ptr") Wraps the given C datatype ptr, registers the two garbage collection routines (see below), and returns a VALUE pointer to a genuine Ruby object. The C type of the resulting object is T_DATA and its Ruby class is class. Data_Make_Struct(VALUE class, c-type, void (*mark)(), void (*free)(), c-type *") Allocates a structure of the indicated type first, then proceeds as Data_Wrap_Struct. c-type is the name of the C datatype that you're wrapping, not a variable of that type. Data_Get_Struct(VALUE obj,c-type,c-type *")
http://www.rubycentral.com/book/ext_ruby.html (7 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns the original pointer. This macro is a type-safe wrapper around the macro DATA_PTR(obj), which evaluates the pointer. The object created by Data_Wrap_Struct is a normal Ruby object, except that it has an additional C datatype that can't be accessed from Ruby. As you can see in Figure 17.1 on page 179, this C datatype is separate from any instance variables that the object contains. But since it's a separate thing, how do you get rid of it when the garbage collector claims this object? What if you have to release some resource (close some file, clean up some lock or IPC mechanism, and so on)? Figure not available... In order to participate in Ruby's mark-and-sweep garbage collection process, you need to define a routine to free your structure, and possibly a routine to mark any references from your structure to other structures. Both routines take a void pointer, a reference to your structure. The mark routine will be called by the garbage collector during its ``mark'' phase. If your structure references other Ruby objects, then your mark function needs to identify these objects using rb_gc_mark(value). If the structure doesn't reference other Ruby objects, you can simply pass 0 as a function pointer. When the object needs to be disposed of, the garbage collector will call the free routine to free it. If you have allocated any memory yourself (for instance, by using Data_Make_Struct), you'll need to pass a free function---even if it's just the standard C library's free routine. For complex structures that you have allocated, your free function may need to traverse the structure to free all the allocated memory. First a simple example, without any special handling. Given the structure definition typedef struct mp3info { char *title; char *artist; int genre; } MP3Info; we can create a structure, populate it, and wrap it as an object.[We cheat a bit in this example. Our MP3Info structure has a couple of char pointers in it. In our code we initialize them from two static strings. This means that we don't have to free these strings when the MP3Info structure is freed. If we'd allocated these strings dynamically, we'd have to write a free method to dispose of them.] MP3Info *p; VALUE info; p = ALLOC(MP3Info); p->artist = "Maynard Ferguson"; p->title = "Chameleon"; ... info = Data_Wrap_Struct(cTest, 0, free, p); info is a VALUE type, a genuine Ruby object of class Test (represented in C by the built-in type T_DATA). You can push it onto an array, hold a reference to it in an object, and so on. At some later point in the code, we may want to access this structure again, given the VALUE:
http://www.rubycentral.com/book/ext_ruby.html (8 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
VALUE doit(VALUE info) { MP3Info *p; Data_Get_Struct(info, MP3Info, p); ... p->artist -> "Maynard Ferguson" p->title -> "Chameleon" ... } In order to follow convention, however, you may need a few more things: support for an initialize method, and a ``C-constructor.'' If you were writing Ruby source, you'd allocate and initialize an object by calling new. In C extensions, the corresponding call is Data_Make_Struct. However, although this allocates memory for the object, it does not automatically call an initialize method; you need to do that yourself: info = Data_Make_Struct(cTest, MP3Info, 0, free, one); rb_obj_call_init(info, argc, argv); This has the benefit of allowing subclasses in Ruby to override or augment the basic initialize in your class. Within initialize, it is allowable (but not necessarily advisable) to alter the existing data pointer, which may be accessed directly with DATA_PTR(obj). And finally, you may want to define a ``C-constructor''---that is, a globally available C function that will create the object in one convenient call. You can use this function within your own code or allow other extension libraries to use it. All of the built-in classes support this idea with functions such as rb_str_new, rb_ary_new, and so on. We can make our own: VALUE mp3_info_new() { VALUE info; MP3Info *one; info = Data_Make_Struct(cTest, MP3Info, 0, free, one); ... rb_obj_call_init(info, 0, 0); return info; }
An Example Okay, now we're ready for a full-size example. Given our vendor's header file above, we write the following code.
http://www.rubycentral.com/book/ext_ruby.html (9 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
#include "ruby.h" #include "cdjukebox.h" VALUE cCDPlayer; static void cd_free(void *p) { CDPlayerDispose(p); } static void progress(CDJukebox *rec, int percent) { if (rb_block_given_p()) { if (percent > 100) percent = 100; if (percent < 0) percent = 0; rb_yield(INT2FIX(percent)); } } static VALUE cd_seek(VALUE self, VALUE disc, VALUE track) { CDJukebox *ptr; Data_Get_Struct(self, CDJukebox, ptr); CDPlayerSeek(ptr, NUM2INT(disc), NUM2INT(track), progress); return Qnil; } static VALUE cd_seekTime(VALUE self) { double tm; CDJukebox *ptr; Data_Get_Struct(self, CDJukebox, ptr); tm = CDPlayerAvgSeekTime(ptr); return rb_float_new(tm); } static VALUE cd_unit(VALUE self) http://www.rubycentral.com/book/ext_ruby.html (10 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
{ return rb_iv_get(self, "@unit"); } static VALUE cd_init(VALUE self, VALUE unit) { rb_iv_set(self, "@unit", unit); return self; } VALUE cd_new(VALUE class, VALUE unit) { VALUE argv[1]; CDJukebox *ptr = CDPlayerNew(NUM2INT(unit)); VALUE tdata = Data_Wrap_Struct(class, 0, cd_free, ptr); argv[0] = unit; rb_obj_call_init(tdata, 1, argv); return tdata; } void Init_CDJukebox() { cCDPlayer = rb_define_class("CDPlayer", rb_cObject); rb_define_singleton_method(cCDPlayer, "new", cd_new, 1); rb_define_method(cCDPlayer, "initialize", cd_init, 1); rb_define_method(cCDPlayer, "seek", cd_seek, 2); rb_define_method(cCDPlayer, "seekTime", cd_seekTime, 0); rb_define_method(cCDPlayer, "unit", cd_unit, 0); } Now we have the ability to control our jukebox from Ruby in a nice, object-oriented manner: require "code/ext/CDJukebox" p = CDPlayer.new(1) puts "Unit is #{p.unit}" p.seek(3, 16) {|x| puts "#{x}% done" } puts "Avg. time was #{p.seekTime} seconds" produces: Unit is 1 26% done 79% done 100% done Avg. time was 1.2 seconds http://www.rubycentral.com/book/ext_ruby.html (11 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
This example demonstrates most of what we've talked about so far, with one additional neat feature. The vendor's library provided a callback routine---a function pointer that is called every so often while the hardware is grinding its way to the next disc. We've set that up here to run a code block passed as an argument to seek. In the progress function, we check to see if there is an iterator in the current context and, if there is, run it with the current percent done as an argument.
Memory Allocation You may sometimes need to allocate memory in an extension that won't be used for object storage---perhaps you've got a giant bitmap for a Bloom filter, or an image, or a whole bunch of little structures that Ruby doesn't use directly. In order to work correctly with the garbage collector, you should use the following memory allocation routines. These routines do a little bit more work than the standard malloc. For instance, if ALLOC_N determines that it cannot allocate the desired amount of memory, it will invoke the garbage collector to try to reclaim some space. It will raise a NoMemError if it can't or if the requested amount of memory is invalid. Memory Allocation type *
type *
type *
ALLOC_N(c-type, n") Allocates n c-type objects, where c-type is the literal name of the C type, not a variable of that type. ALLOC(c-type") Allocates a c-type and casts the result to a pointer of that type. REALLOC_N(var, c-type, n") Reallocates n c-types and assigns the result to var, a pointer to a c-type. ALLOCA_N(c-type, n") Allocates memory for n objects of c-type on the stack---this memory will be automatically freed when the function that invokes ALLOCA_N returns.
Creating an Extension Having written the source code for an extension, we now need to compile it so Ruby can use it. We can either do this as a shared object, which is dynamically loaded at runtime, or statically link the extension into the main Ruby interpreter itself. The basic procedure is the same: ● Create the C source code file(s) in a given directory. ● Create extconf.rb. ● Run extconf.rb to create a Makefile for the C files in this directory. ● Run make. ● Run make install. Figure not available...
http://www.rubycentral.com/book/ext_ruby.html (12 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Creating a Makefile with extconf.rb Figure 17.2 on page 184 shows the overall workflow when building an extension. The key to the whole process is the extconf.rb program which you, as a developer, create. In extconf.rb, you write a simple program that determines what features are available on the user's system and where those features may be located. Executing extconf.rb builds a customized Makefile, tailored for both your application and the system on which it's being compiled. When you run the make command against this Makefile, your extension is built and (optionally) installed. The simplest extconf.rb may be just two lines long, and for many extensions this is sufficient. require 'mkmf' create_makefile("Test") The first line brings in the mkmf library module (documented beginning on page 455). This contains all the commands we'll be using. The second line creates a Makefile for an extension called ``Test.'' (Note that ``Test'' is the name of the extension; the makefile will always be called ``Makefile.'') Test will be built from all the C source files in the current directory. Let's say that we run this extconf.rb program in a directory containing a single source file, main.c. The result is a Makefile that will build our extension. On our system, this contains the following commands. gcc -fPIC -I/usr/local/lib/ruby/1.6/i686-linux -g -O2 -c main.c -o main.o gcc -shared -o Test.so main.o -lc
\
The result of this compilation is Test.so, which may be dynamically linked into Ruby at runtime with ``require''. See how the mkmf commands have located platform-specific libraries and used compiler-specific options automatically. Pretty neat, eh? Although this basic program works for many simple extensions, you may have to do some more work if your extension needs header files or libraries that aren't included in the default compilation environment, or if you conditionally compile code based on the presence of libraries or functions. A common requirement is to specify nonstandard directories where include files and libraries may be found. This is a two-step process. First, your extconf.rb should contain one or more dir_config commands. This specifies a tag for a set of directories. Then, when you run the extconf.rb program, you tell mkmf where the corresponding physical directories are on the current system. If extconf.rb contains the line dir_config(name), then you give the location of the corresponding directories with the command-line options: --with-name-include=directory * Add directory/include to the compile command. --with-name-lib=directory * Add directory/lib to the link command. http://www.rubycentral.com/book/ext_ruby.html (13 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
If (as is common) your include and library directories are both subdirectories of some other directory, and (as is also common) they're called include and lib, you can take a shortcut: --with-name-dir=directory * Add directory/lib and directory/include to the link command and compile command, respectively. There's a twist here. As well as specifying all these --with options when you run extconf.rb, you can also use the --with options that were specified when Ruby was built for your machine. This means you can find out the locations of libraries that are used by Ruby itself. To make all this concrete, lets say you need to use libraries and include files for the CD jukebox we're developing. Your extconf.rb program might contain require 'mkmf' dir_config('cdjukebox') # .. more stuff create_makefile("CDJukeBox") You'd then run extconf.rb with something like: % ruby extconf.rb --with-cdjukebox-dir=/usr/local/cdjb The generated Makefile would assume that the libraries were in /usr/local/cdjb/lib and the include files were in /usr/local/cdjb/include. The dir_config command adds to the list of places to search for libraries and include files. It does not, however, link the libraries into your application. To do that, you'll need to use one or more have_library or find_library commands. have_library looks for a given entry point in a named library. If it finds the entry point, it adds the library to the list of libraries to be used when linking your extension. find_library is similar, but allows you to specify a list of directories to search for the library. require 'mkmf' dir_config('cdjukebox') have_library('cdjb', 'CDPlayerNew') create_makefile("CDJukeBox") On some platforms, a popular library may be in one of several places. The X Window system, for example, is notorious for living in different directories on different systems. The find_library command will search a list of supplied directories to find the right one (this is different from have_library, which uses only configuration information for the search). For example, to create a Makefile that uses X Windows and a jpeg library, extconf.rb might contain
http://www.rubycentral.com/book/ext_ruby.html (14 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
require 'mkmf' if have_library("jpeg","jpeg_mem_init") and find_library("X11", "XOpenDisplay", "/usr/X11/lib", "/usr/X11R6/lib", "/usr/openwin/lib") then create_makefile("XThing") else puts "No X/JPEG support available" end We've added some additional functionality to this program. All of the mkmf commands return false if they fail. This means that we can write an extconf.rb that generates a Makefile only if everything it needs is present. The Ruby distribution does this so that it will try to compile only those extensions that are supported on your system. You also may want your extension code to be able to configure the features it uses depending on the target environment. For example, our CD jukebox may be able to use a high-performance MP3 decoder if the end user has one installed. We can check by looking for its header file. require 'mkmf' dir_config('cdjukebox') have_library('cdjb', 'CDPlayerNew') have_header('hp_mp3.h') create_makefile("CDJukeBox") We can also check to see if the target environment has a particular function in any of the libraries we'll be using. For example, the setpriority call would be useful but isn't always available. We can check for it with: require 'mkmf' dir_config('cdjukebox') have_func('setpriority') create_makefile("CDJukeBox") Both have_header and have_func define preprocessor constants if they find their targets. The names are formed by converting the target name to uppercase and prepending ``HAVE_''. Your C code can take advantage of this using constructs such as:
http://www.rubycentral.com/book/ext_ruby.html (15 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
#if defined(HAVE_HP_MP3_H) # include #endif #if defined(HAVE_SETPRIORITY) err = setpriority(PRIOR_PROCESS, 0, -10) #endif If you have special requirements that can't be met with all these mkmf commands, your program can directly add to the global variables $CFLAGS and $LFLAGS, which are passed to the compiler and linker, respectively.
Static Linking Finally, if your system doesn't support dynamic linking, or if you have an extension module that you want to have statically linked into Ruby itself, edit the file ext/Setup in the distribution and add your directory to the list of extensions in the file, then rebuild Ruby. The extensions listed in Setup will be statically linked into the Ruby executable. If you want to disable any dynamic linking, and link all extensions statically, edit ext/Setup to contain the following option. option nodynamic
Embedding a Ruby Interpreter In addition to extending Ruby by adding C code, you can also turn the problem around and embed Ruby itself within your application. Here's an example. #include "ruby.h" main() { /* ... our own application stuff ... */ ruby_init(); ruby_script("embedded"); rb_load_file("start.rb"); while (1) { if (need_to_do_ruby) { ruby_run(); } /* ... run our app stuff */ } } To initialize the Ruby interpreter, you need to call ruby_init(). But on some platforms, you may
http://www.rubycentral.com/book/ext_ruby.html (16 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
need to take special steps before that: #if defined(NT) NtInitialize(&argc, &argv); #endif #if defined(__MACOS__) && defined(__MWERKS__) argc = ccommand(&argv); #endif See main.c in the Ruby distribution for any other special defines or setup needed for your platform. Embedded Ruby API void
void void void void
ruby_init(") Sets up and initializes the interpreter. This function should be called before any other Ruby-related functions. ruby_options(int argc, char **argv") Gives the Ruby interpreter the command-line options. ruby_script(char *name") Sets the name of the Ruby script (and $0) to name. rb_load_file(char *file") Loads the given file into the interpreter. ruby_run(") Runs the interpreter.
You need to take some special care with exception handling; any Ruby calls you make at this top level should be protected to catch exceptions and handle them cleanly. rb_protect, rb_rescue, and related functions are documented on page 194. For an example of embedding a Ruby interpreter within another program, see also eruby, which is described beginning on page 149.
Bridging Ruby to Other Languages So far, we've discussed extending Ruby by adding routines written in C. However, you can write extensions in just about any language, as long as you can bridge the two languages with C. Almost anything is possible, including awkward marriages of Ruby and C++, Ruby and Java, and so on. But you may be able to accomplish the same thing without resorting to C code. For example, you could bridge to other languages using middleware such as CORBA or COM. See the section on Windows automation beginning on page 166 for more details.
http://www.rubycentral.com/book/ext_ruby.html (17 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Ruby C Language API Last, but by no means least, here are several C-level functions that you may find useful when writing an extension. Some functions require an ID: you can obtain an ID for a string by using rb_intern and reconstruct the name from an ID by using rb_id2name. As most of these C functions have Ruby equivalents that are already described in detail elsewhere in this book, the descriptions here will be brief. Also note that the following listing is not complete. There are many more functions available---too many to document them all, as it turns out. If you need a method that you can't find here, check ``ruby.h'' or ``intern.h'' for likely candidates. Also, at or near the bottom of each source file is a set of method definitions that describe the binding from Ruby methods to C functions. You may be able to call the C function directly, or search for a wrapper function that calls the function you are looking for. The following list, based on the list in README.EXT, shows the main source files in the interpreter. Ruby Language Core class.c error.c eval.c gc.c object.c parse.y variable.c Utility Functions dln.c regex.c st.c util.c Ruby Interpreter dmyext.c inits.c keywords main.c ruby.c version.c Base Library array.c bignum.c compar.c dir.c enum.c file.c hash.c io.c marshal.c math.c numeric.c pack.c prec.c process.c random.c range.c re.c signal.c sprintf.c string.c struct.c time.c VALUE
VALUE
VALUE
VALUE void
Defining Objects rb_define_class(char *name, VALUE superclass") Defines a new class at the top level with the given name and superclass (for class Object, use rb_cObject). rb_define_module(char *name") Defines a new module at the top level with the given name. rb_define_class_under(VALUE under, char *name, VALUE superclass") Defines a nested class under the class or module under. rb_define_module_under(VALUE under, char *name") Defines a nested module under the class or module under. rb_include_module(VALUE parent, VALUE module")
http://www.rubycentral.com/book/ext_ruby.html (18 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
void VALUE
Includes the given module into the class or module parent. rb_extend_object(VALUE obj, VALUE module") Extends obj with module. rb_require(const char *name") Equivalent to ``require name.'' Returns Qtrue or Qfalse.
In some of the function definitions that follow, the parameter argc specifies how many arguments a Ruby method takes. It may have the following values. argc Function prototype 0..17 VALUE func(VALUE self, VALUE arg...) The C function will be called with this many actual arguments. -1 VALUE func(int argc, VALUE *argv, VALUE self) The C function will be given a variable number of arguments passed as a C array. -2 VALUE func(VALUE self, VALUE args) The C function will be given a variable number of arguments passed as a Ruby array. In a function that has been given a variable number of arguments, you can use the C function rb_scan_args to sort things out (see below).
void
void
void
void
int
Defining Methods rb_define_method(VALUE classmod, char *name, VALUE(*func)(), int argc") Defines an instance method in the class or module classmod with the given name, implemented by the C function func and taking argc arguments. rb_define_module_function(VALUE classmod, char *name, VALUE(*func)(), int argc)") Defines a method in class classmod with the given name, implemented by the C function func and taking argc arguments. rb_define_global_function(char *name, VALUE(*func)(), int argc") Defines a global function (a private method of Kernel) with the given name, implemented by the C function func and taking argc arguments. rb_define_singleton_method(VALUE classmod, char *name, VALUE(*func)(), int argc") Defines a singleton method in class classmod with the given name, implemented by the C function func and taking argc arguments. rb_scan_args(int argcount, VALUE *argv, char *fmt, ...")
http://www.rubycentral.com/book/ext_ruby.html (19 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Scans the argument list and assigns to variables similar to scanf: fmt is a string containing zero, one, or two digits followed by some flag characters. The first digit indicates the count of mandatory arguments; the second is the count of optional arguments. A ``*'' means to pack the rest of the arguments into a Ruby array. A ``&'' means that an attached code block will be taken and assigned to the given variable (if no code block was given, Qnil will be assigned). After the fmt string, pointers to VALUE are given (as with scanf) to which the arguments are assigned. VALUE name, one, two, rest; rb_scan_args(argc, argv, "12", &name, &one, &two); rb_scan_args(argc, argv, "1*", &name, &rest); void
void
void void void
void
void
rb_undef_method(VALUE classmod, const char *name") Undefines the given method name in the given classmod class or module. rb_define_alias(VALUE classmod, const char *newname, const char *oldname") Defines an alias for oldname in class or module classmod. Defining Variables and Constants rb_define_const(VALUE classmod, char *name, VALUE value") Defines a constant in the class or module classmod, with the given name and value. rb_define_global_const(char *name, VALUE value") Defines a global constant with the given name and value. rb_define_variable(const char *name, VALUE *object") Exports the address of the given object that was created in C to the Ruby namespace as name. From Ruby, this will be a global variable, so name should start with a leading dollar sign. Be sure to honor Ruby's rules for allowed variable names; illegally named variables will not be accessible from Ruby. rb_define_class_variable(VALUE class, const char *name, VALUE val") Defines a class variable name (which must be specified with a ``@@'' prefix) in the given class, initialized to value. rb_define_virtual_variable(const char *name, VALUE(*getter)(), void(*setter)()")
http://www.rubycentral.com/book/ext_ruby.html (20 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Exports a virtual variable to Ruby namespace as the global $name. No actual storage exists for the variable; attempts to get and set the value will call the given functions with the prototypes: VALUE getter(ID id, VALUE *data, struct global_entry *entry); void setter(VALUE value, ID id, VALUE *data, struct global_entry *entry); You will likely not need to use the entry parameter and can safely omit it from your function declarations. rb_define_hooked_variable(const char *name, VALUE *variable, void VALUE(*getter)(), void(*setter)()") Defines functions to be called when reading or writing to variable. See also rb_define_virtual_variable. void rb_define_readonly_variable(const char *name, VALUE *value") Same as rb_define_variable, but read-only from Ruby. rb_define_attr(VALUE variable, const char *name, int read, void int write") Creates accessor methods for the given variable, with the given name. If read is nonzero, create a read method; if write is nonzero, create a write method. void rb_global_variable(VALUE *obj") Registers the given address with the garbage collector. Calling Methods VALUE rb_funcall(VALUE recv, ID id, int argc, ...") Invokes the method given by id in the object recv with the given number of arguments argc and the arguments themselves (possibly none). VALUE rb_funcall2(VALUE recv, ID id, int argc, VALUE *args") Invokes the method given by id in the object recv with the given number of arguments argc and the arguments themselves given in the C array args. VALUE rb_funcall3(VALUE recv, ID id, int argc, VALUE *args") Same as rb_funcall2, but will not call private methods. VALUE rb_apply(VALUE recv, ID name, int argc, VALUE args") Invokes the method given by id in the object recv with the given number of arguments argc and the arguments themselves given in the Ruby Array args. ID rb_intern(char *name") Returns an ID for a given name. If the name does not exist, a symbol table entry will be created for it. char * rb_id2name(ID id") Returns a name for the given id. VALUE rb_call_super(int argc, VALUE *args") http://www.rubycentral.com/book/ext_ruby.html (21 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
void
void
void
void
VALUE
VALUE
VALUE void
void
void
void
Calls the current method in the superclass of the current object. Exceptions rb_raise(VALUE exception, const char *fmt, ...") Raises an exception. The given string fmt and remaining arguments are interpreted as with printf. rb_fatal(const char *fmt, ...") Raises a Fatal exception, terminating the process. No rescue blocks are called, but ensure blocks will be called. The given string fmt and remaining arguments are interpreted as with printf. rb_bug(const char *fmt, ...") Terminates the process immediately---no handlers of any sort will be called. The given string fmt and remaining arguments are interpreted as with printf. You should call this function only if a fatal bug has been exposed. You don't write fatal bugs, do you? rb_sys_fail(const char *msg") Raises a platform-specific exception corresponding to the last known system error, with the given msg. rb_rescue(VALUE (*body)(), VALUE args, VALUE(*rescue)(), VALUE rargs") Executes body with the given args. If a StandardError exception is raised, then execute rescue with the given rargs. rb_ensure(VALUE(*body)(), VALUE args, VALUE(*ensure)(), VALUE eargs") Executes body with the given args. Whether or not an exception is raised, execute ensure with the given rargs after body has completed. rb_protect(VALUE (*body)(), VALUE args, int *result") Executes body with the given args and returns nonzero in result if any exception was raised. rb_notimplement(") Raises a NotImpError exception to indicate that the enclosed function is not implemented yet, or not available on this platform. rb_exit(int status") Exits Ruby with the given status. Raises a SystemExit exception and calls registered exit functions and finalizers. rb_warn(const char *fmt, ...") Unconditionally issues a warning message to standard error. The given string fmt and remaining arguments are interpreted as with printf. rb_warning(const char *fmt, ...") Conditionally issues a warning message to standard error if Ruby was invoked with the -w flag. The given string fmt and remaining arguments are interpreted as with printf. Iterators
http://www.rubycentral.com/book/ext_ruby.html (22 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
void VALUE VALUE
int
VALUE
VALUE void
VALUE
VALUE VALUE
VALUE VALUE VALUE void VALUE int
rb_iter_break(") Breaks out of the enclosing iterator block. rb_each(VALUE obj") Invokes the each method of the given obj. rb_yield(VALUE arg") Transfers execution to the iterator block in the current context, passing arg as an argument. Multiple values may be passed in an array. rb_block_given_p(") Returns true if yield would execute a block in the current context---that is, if a code block was passed to the current method and is available to be called. rb_iterate(VALUE (*method)(), VALUE args, VALUE (*block)(), VALUE arg2") Invokes method with argument args and block block. A yield from that method will invoke block with the argument given to yield, and a second argument arg2. rb_catch(const char *tag, VALUE (*proc)(), VALUE value") Equivalent to Ruby catch. rb_throw(const char *tag , VALUE value") Equivalent to Ruby throw. Accessing Variables rb_iv_get(VALUE obj, char *name") Returns the instance variable name (which must be specified with a ``@'' prefix) from the given obj. rb_ivar_get(VALUE obj, ID name") Returns the instance variable name from the given obj. rb_iv_set(VALUE obj, char *name, VALUE value") Sets the value of the instance variable name (which must be specified with a ``@'' prefix) in the given obj to value. Returns value. rb_ivar_set(VALUE obj, ID name, VALUE value") Sets the value of the instance variable name in the given obj to value. Returns value. rb_gv_set(const char *name, VALUE value") Sets the global variable name (the ``$'' prefix is optional) to value. Returns value. rb_gv_get(const char *name") Returns the global variable name (the ``$'' prefix is optional). rb_cvar_set(VALUE class, ID name, VALUE val") Sets the class variable name in the given class to value. rb_cvar_get(VALUE class, ID name") Returns the class variable name from the given class. rb_cvar_defined(VALUE class, ID name")
http://www.rubycentral.com/book/ext_ruby.html (23 of 26) [7/20/2001 9:49:24 PM]
Programming Ruby: The Pragmatic Programmer's Guide
void
VALUE
int
int
int void void
VALUE VALUE VALUE VALUE void
Returns Qtrue if the given class variable name has been defined for class; otherwise, returns Qfalse. rb_cv_set(VALUE class, const char *name, VALUE val") Sets the class variable name (which must be specified with a ``@@'' prefix) in the given class to value. rb_cv_get(VALUE class, const char *name") Returns the class variable name (which must be specified with a ``@@'' prefix) from the given class. Object Status OBJ_TAINT(VALUE obj") Marks the given obj as tainted. OBJ_TAINTED(VALUE obj") Returns nonzero if the given obj is tainted. OBJ_FREEZE(VALUE obj") Marks the given obj as frozen. OBJ_FROZEN(VALUE obj") Returns nonzero if the given obj is frozen. Check_SafeStr(VALUE str") Raises SecurityError if current safe level > 0 and str is tainted, or a TypeError if str is not a T_STRING. rb_safe_level(") Returns the current safe level. rb_secure(int level") Raises SecurityError if level .*b).*a/ } end produces:
Normal: Nested:
user 2.620000 0.000000
system 0.000000 0.000000
total 2.620000 ( 0.000000 (
real 2.651591) 0.000883)
(?imx) Turns on the corresponding ``i,'' ``m,'' or ``x'' option. If used inside a group, the effect is limited to that group. (?-imx) Turns off the ``i,'' ``m,'' or ``x'' option. (?imx:re) Turns on the ``i,'' ``m,'' or ``x'' option for re. (?-imx:re) Turns off the ``i,'' ``m,'' or ``x'' option for re.
Names Ruby names are used to refer to constants, variables, methods, classes, and modules. The first character of a name helps Ruby to distinguish its intended use. Certain names, listed in Table 18.3 on page 212, are reserved words and should not be used as variable, method, class, or module names. Reserved words __FILE__ __LINE__ BEGIN END alias
and begin break case class
def defined? do else elsif
end ensure false for if
in module next nil not
or redo rescue retry return
self super then true undef
unless until when while yield
In these descriptions, lowercase letter means the characters ``a'' though ``z'', as well as ``_'', the underscore. Uppercase letter means ``A'' though ``Z,'' and digit means ``0'' through ``9.'' Name characters means any combination of upper- and lowercase letters and digits. A local variable name consists of a lowercase letter followed by name characters. http://www.rubycentral.com/book/language.html (11 of 39) [7/20/2001 9:49:31 PM]
Programming Ruby: The Pragmatic Programmer's Guide
fred
anObject
_x
three_two_one
An instance variable name starts with an ``at'' sign (``@'') followed by an upper- or lowercase letter, optionally followed by name characters. @name
@_
@Size
A class variable name starts with two ``at'' signs (``@@'') followed by an upper- or lowercase letter, optionally followed by name characters. @@name
@@_
@@Size
A constant name starts with an uppercase letter followed by name characters. Class names and module names are constants, and follow the constant naming conventions. By convention, constant variables are normally spelled using uppercase letters and underscores throughout. module Math PI = 3.1415926 end class BigBlob Global variables, and some special system variables, start with a dollar sign (``$'') followed by name characters. In addition, there is a set of two-character variable names in which the second character is a punctuation character. These predefined variables are listed starting on page 216. Finally, a global variable name can be formed using ``$-'' followed by any single character. $params
$PROGRAM
$!
$_
$-a
$-.
Method names are described in the section beginning on page 227.
Variable/Method Ambiguity When Ruby sees a name such as ``a'' in an expression, it needs to determine if it is a local variable reference or a call to a method with no parameters. To decide which is the case, Ruby uses a heuristic. As Ruby reads a source file, it keeps track of symbols that have been assigned to. It assumes that these symbols are variables. When it subsequently comes across a symbol that might be either a variable or a method call, it checks to see if it has seen a prior assignment to that symbol. If so, it treats the symbol as a variable; otherwise it treats it as a method call. As a somewhat pathological case of this, consider the following code fragment, submitted by Clemens Hintze.
http://www.rubycentral.com/book/language.html (12 of 39) [7/20/2001 9:49:31 PM]
Programming Ruby: The Pragmatic Programmer's Guide
def a print "Function 'a' called\n" 99 end for i in 1..2 if i == 2 print "a=", a, "\n" else a = 1 print "a=", a, "\n" end end produces: a=1 Function 'a' called a=99 During the parse, Ruby sees the use of ``a'' in the first print statement and, as it hasn't yet seen any assignment to ``a,'' assumes that it is a method call. By the time it gets to the second print statement, though, it has seen an assignment, and so treats ``a'' as a variable. Note that the assignment does not have to be executed---Ruby just has to have seen it. This program does not raise an error. a = 1 if false; a
Variables and Constants Ruby variables and constants hold references to objects. Variables themselves do not have an intrinsic type. Instead, the type of a variable is defined solely by the messages to which the object referenced by the variable responds.[When we say that a variable is not typed, we mean that any given variable can at different times hold references to objects of many different types.] A Ruby constant is also a reference to an object. Constants are created when they are first assigned to (normally in a class or module definition). Ruby, unlike less flexible languages, lets you alter the value of a constant, although this will generate a warning message. MY_CONST = 1 MY_CONST = 2
# generates a warning
produces:
http://www.rubycentral.com/book/language.html (13 of 39) [7/20/2001 9:49:31 PM]
Programming Ruby: The Pragmatic Programmer's Guide
prog.rb:2: warning: already initialized constant MY_CONST Note that although constants should not be changed, you can alter the internal states of the objects they reference. MY_CONST = "Tim" MY_CONST[0] = "J" MY_CONST
# alter string referenced by constant » "Jim"
Assignment potentially aliases objects, giving the same object different names.
Scope of Constants and Variables Constants defined within a class or module may be accessed unadorned anywhere within the class or module. Outside the class or module, they may be accessed using the scope operator, ``::'' prefixed by an expression that returns the appropriate class or module object. Constants defined outside any class or module may be accessed unadorned or by using the scope operator ``::'' with no prefix. Constants may not be defined in methods. OUTER_CONST = 99 class Const def getConst CONST end CONST = OUTER_CONST + 1 end Const.new.getConst Const::CONST ::OUTER_CONST
» » »
100 100 99
Global variables are available throughout a program. Every reference to a particular global name returns the same object. Referencing an uninitialized global variable returns nil. Class variables are available throughout a class or module body. Class variables must be initialized before use. A class variable is shared among all instances of a class and is available within the class itself.
http://www.rubycentral.com/book/language.html (14 of 39) [7/20/2001 9:49:31 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class Song @@count = 0 def initialize @@count += 1 end def Song.getCount @@count end end Class variables belong to the innermost enclosing class or module. Class variables used at the top level are defined in Object, and behave like global variables. Class variables defined within singleton methods belong to the receiver if the receiver is a class or a module; otherwise, they belong to the class of the receiver. class Holder @@var = 99 def Holder.var=(val) @@var = val end end a = Holder.new def a.var @@var end Holder.var = 123 a.var
»
123
Instance variables are available within instance methods throughout a class body. Referencing an uninitialized instance variable returns nil. Each instance of a class has a unique set of instance variables. Instance variables are not available to class methods. Local variables are unique in that their scopes are statically determined but their existence is established dynamically. A local variable is created dynamically when it is first assigned a value during program execution. However, the scope of a local variable is statically determined to be: the immediately enclosing block, method definition, class definition, module definition, or top-level program. Referencing a local variable that is in scope but that has not yet been created generates a NameError exception. Local variables with the same name are different variables if they appear in disjoint scopes. Method parameters are considered to be variables local to that method. Block parameters are assigned values when the block is invoked.
http://www.rubycentral.com/book/language.html (15 of 39) [7/20/2001 9:49:31 PM]
Programming Ruby: The Pragmatic Programmer's Guide
a = [ 1, 2, 3 ] a.each { |i| puts i a.each { |$i| puts $i a.each { |@i| puts @i a.each { |I| puts I a.each { |b.meth| } sum = 0 var = nil a.each { |var| sum +=
} } } }
# # # # #
var }
i local to block assigns to global $i assigns to instance variable @i generates warning assigning to constant invokes meth= in object b
# uses sum and var from enclosing scope
If a local variable (including a block parameter) is first assigned in a block, it is local to the block. If instead a variable of the same name is already established at the time the block executes, the block will inherit that variable. A block takes on the set of local variables in existence at the time that it is created. This forms part of its binding. Note that although the binding of the variables is fixed at this point, the block will have access to the current values of these variables when it executes. The binding preserves these variables even if the original enclosing scope is destroyed. The bodies of while, until, and for loops are part of the scope that contains them; previously existing locals can be used in the loop, and any new locals created will be available outside the bodies afterward.
Predefined Variables Predefined Variables The following variables are predefined in the Ruby interpreter. In these descriptions, the notation {} indicates that the variables are read-only; an error will be raised if a program attempts to modify a read-only variable. After all, you probably don't want to change the meaning of true halfway through your program (except perhaps if you're a politician). Entries marked {} are thread local. Many global variables look something like Snoopy swearing: $_, $!, $&, and so on. This is for ``historical'' reasons, as most of these variable names come from Perl. If you find memorizing all this punctuation difficult, you might want to have a look at the library file called ``English,'' documented on page 449, which gives the commonly used global variables more descriptive names. In the tables of variables and constants that follow, we show the variable name, the type of the referenced object, and a description.
Exception Information $! $@
Exception The exception object passed to raise. {} The stack backtrace generated by the last exception. See Kernel#caller on Array page 417 for details. {}
http://www.rubycentral.com/book/language.html (16 of 39) [7/20/2001 9:49:31 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Pattern Matching Variables These variables (except $=) are set to nil after an unsuccessful pattern match. $& $+ $` $' $=
$1 to $9
$~
The string matched by the last successful pattern match. This variable is local to the current scope. {} The contents of the highest-numbered group matched in the last successful pattern String match. Thus, in "cat" =~/(c|a)(t|z)/, $+ will be set to ``t''. This variable is local to the current scope. {} The string preceding the match in the last successful pattern match. This variable String is local to the current scope. {} The string following the match in the last successful pattern match. This variable String is local to the current scope. {} If set to any value apart from nil or false, all pattern matches will be case Object insensitive, string comparisons will ignore case, and string hash values will be case insensitive. The contents of successive groups matched in the last successful pattern match. In String "cat" =~/(c|a)(t|z)/, $1 will be set to ``a'' and $2 to ``t''. This variable is local to the current scope. {} An object that encapsulates the results of a successful pattern match. The variables $&, $`, $', and $1 to $9 are all derived from $~. Assigning to $~ MatchData changes the values of these derived variables. This variable is local to the current scope. {} String
Input/Output Variables $/
String
$-0
String
$\
String
$,
String
$.
Fixnum
$;
String
The input record separator (newline by default). This is the value that routines such as Kernel#gets use to determine record boundaries. If set to nil, gets will read the entire file. Synonym for $/. The string appended to the output of every call to methods such as Kernel#print and IO#write. The default value is nil. The separator string output between the parameters to methods such as Kernel#print and Array#join. Defaults to nil, which adds no text. The number of the last line read from the current input file. The default separator pattern used by String#split. May be set from the command line using the -F flag.
http://www.rubycentral.com/book/language.html (17 of 39) [7/20/2001 9:49:31 PM]
Programming Ruby: The Pragmatic Programmer's Guide
$
IO
$_
String
$defout $-F $stderr $stdin $stdout
IO String IO IO IO
An object that provides access to the concatenation of the contents of all the files given as command-line arguments, or $stdin (in the case where there are no arguments). $< supports methods similar to a File object: binmode, close, closed?, each, each_byte, each_line, eof, eof?, file, filename, fileno, getc, gets, lineno, lineno=, pos, pos=, read, readchar, readline, readlines, rewind, seek, skip, tell, to_a, to_i, to_io, to_s, along with the methods in Enumerable. The method file returns a File object for the file currently being read. This may change as $< reads through the files on the command line. {} The destination of output for Kernel#print and Kernel#printf. The default value is $stdout. The last line read by Kernel#gets or Kernel#readline. Many string-related functions in the Kernel module operate on $_ by default. The variable is local to the current scope. {} Synonym for $>. Synonym for $;. The current standard error output. The current standard input. The current standard output.
Execution Environment Variables The name of the top-level Ruby program being executed. Typically this will be the program's filename. On some operating systems, assigning to this variable will $0 String change the name of the process reported (for example) by the ps(1) command. An array of strings containing the command-line options from the invocation of $* Array the program. Options used by the Ruby interpreter will have been removed. {} $" Array An array containing the filenames of modules loaded by require. {} $$ Fixnum The process number of the program being executed. {} $? Fixnum The exit status of the last child process to terminate. {} An array of strings, where each string specifies a directory to be searched for Ruby scripts and binary extensions used by the load and require methods. The initial value is the value of the arguments passed via the -I command-line $: Array option, followed by an installation-defined standard library location, followed by the current directory (``.''). This variable may be set from within a program to alter the default search path; typically, programs use $: > 0=
Y
0== 0=== 0!= 0=~ 0!~
&& || .. 0... ? : = 0%= 0{ /= 0--= 0+= 0|= &= 0>>= 0 'cat', 2 => 'dog', *anArray, &aProc) produces: a = test b = {1=>"cat", 2=>"dog"} c = 98 d = [97, 96] block = 99 A method is called by passing its name to a receiver. If no receiver is specified, self is assumed. The receiver checks for the method definition in its own class and then sequentially in its ancestor classes. The instance methods of included modules act as if they were in anonymous superclasses of the class that includes them. If the method is not found, Ruby invokes the method method_missing in the receiver. The default behavior defined in Kernel::method_missing is to report an error and terminate the program. When a receiver is explicitly specified in a method invocation, it may be separated from the method name using either a period ``.'' or two colons ``::''. The only difference between these two forms occurs if the method name starts with an uppercase letter. In this case, Ruby will assume that a receiver::Thing method call is actually an attempt to access a constant called Thing in the receiver unless the method invocation has a parameter list between parentheses. Foo.Bar() Foo.Bar Foo::Bar() Foo::Bar
# # # #
method call method call method call constant access
The return value of a method is the value of the last expression executed. return [expr]* A return expression immediately exits a method. The value of a return is nil if it is called with no parameters, the value of its parameter if it is called with one parameter, or an array containing all of its parameters if it is called with more than one parameter.
http://www.rubycentral.com/book/language.html (31 of 39) [7/20/2001 9:49:31 PM]
Programming Ruby: The Pragmatic Programmer's Guide
super super
[ ( [param]* [*array] ) ]
[block]
Within the body of a method, a call to super acts just like a call to that original method, except that the search for a method body starts in the superclass of the object that was found to contain the original method. If no parameters (and no parentheses) are passed to super, the original method's parameters will be passed; otherwise, the parameters to super will be passed.
Operator Methods expr1 operator operator expr1 expr1 operator expr2 If the operator in an operator expression corresponds to a redefinable method (see the Table 18.4 on page 221), Ruby will execute the operator expression as if it had been written (expr1).operator(expr2)
Attribute Assignment receiver.attrname = rvalue When the form receiver.attrname appears as an lvalue, Ruby invokes a method named attrname= in the receiver, passing rvalue as a single parameter.
Element Reference Operator receiver [expr]+ receiver [expr]+
= rvalue
When used as an rvalue, element reference invokes the method [] in the receiver, passing as parameters the expressions between the brackets. When used as an lvalue, element reference invokes the method []= in the receiver, passing as parameters the expressions between the brackets, followed by the rvalue being assigned.
Aliasing alias newName oldName http://www.rubycentral.com/book/language.html (32 of 39) [7/20/2001 9:49:31 PM]
Programming Ruby: The Pragmatic Programmer's Guide
creates a new name that refers to an existing method, operator, global variable, or regular expression backreference ($&, $', $', and $+). Local variables, instance variables, class variables, and constants may not be aliased. The parameters to alias may be names or symbols. class Fixnum alias plus + end 1.plus(3)
» 4
alias $prematch $` » 3 "string" =~ /i/ » "str" $prematch alias :cmd :` cmd "date"
» "Sun Mar
4 23:24:32 CST 2001\n"
When a method is aliased, the new name refers to a copy of the original method's body. If the method is subsequently redefined, the aliased name will still invoke the original implementation. def meth "original method" end alias original meth def meth "new and improved" end » "new and improved" meth » "original method" original
Class Definition class classname body end
[< superexpr]
class anArray Returns an array of the names of all constants defined in the system. This list includes the names of all modules and classes. p Module.constants.sort[1..5] produces: ["ARGV", "ArgumentError", "Array", "Bignum", "Binding"]
http://www.rubycentral.com/book/ref_c_module.html (1 of 10) [7/20/2001 9:49:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
nesting
Module.nesting -> anArray
Returns the list of Modules nested at the point of call. module M1 module M2 $a = Module.nesting end end $a $a[0].name
new
» »
[M1::M2, M1] "M1::M2"
Module.new -> aModule Creates a new anonymous module.
instance methods =
mod relop aModule -> true or false Hierarchy Query---One module is considered greater than another if it is included in (or is a parent class of) the other module. The other operators are defined accordingly. If there is no relationship between the modules, returns false for all operators. module Mixin end module Parent include Mixin end module Unrelated end Parent Parent Parent Parent Parent
> Mixin < Mixin Unrelated
» » » » »
false true true false false
mod aModule -> -1, 0, +1 Comparison---Returns -1 if mod includes aModule, 0 if mod is the same as aModule, and +1 if mod is included by aModule or if mod has no relationship with aModule.
===
mod === anObject -> true or false Case Equality---Returns true if anObject is an instance of mod or one of mod's descendents. Of limited use for modules, but can be used in case statements to classify objects by class.
http://www.rubycentral.com/book/ref_c_module.html (2 of 10) [7/20/2001 9:49:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
ancestors
mod.ancestors -> anArray Returns a list of modules included in mod (including mod itself). module Mod include Math include Comparable end Mod.ancestors Math.ancestors
class_eval
» [Mod, Comparable, Math] » [Math]
mod.class_eval( aString ) -> anObject mod.class_eval {| | block } -> anObject Synonym for Module.module_eval.
class_variables
mod.class_variables -> anArray Returns an array of the names of class variables in mod and the ancestors of mod. class One @@var1 = 1 end class Two < One @@var2 = 2 end One.class_variables Two.class_variables
clone
» ["@@var1"] » ["@@var2", "@@var1"]
mod.clone -> aModule Creates a new copy of a module. m = Math.clone m.constants m == Math
const_defined?
» » »
Math ["PI", "E"] false
mod.const_defined?( aSymbol ) -> true or false Returns true if a constant with the given name is defined by mod. Math.const_defined? "PI"
const_get
» true
mod.const_get( aSymbol ) -> anObject Returns the value of the named constant in mod. Math.const_get :PI
const_set
mod.const_set( aSymbol, anObject ) -> anObject
http://www.rubycentral.com/book/ref_c_module.html (3 of 10) [7/20/2001 9:49:35 PM]
» 3.141592654
Programming Ruby: The Pragmatic Programmer's Guide
Sets the named constant to the given object, returning that object. Creates a new constant if no constant with the given name previously existed. Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) » 3.142857143 » 0.001264489267 Math::HIGH_SCHOOL_PI - Math::PI
constants
mod.constants -> anArray Returns an array of the names of the constants accessible in mod. This includes the names of constants in any included modules (example at start of section).
included_modules
mod.included_modules -> anArray Returns the list of modules included in mod. module Mixin end module Outer include Mixin end Mixin.included_modules Outer.included_modules
instance_methods
» [] » [Mixin]
mod.instance_methods( includeSuper=false ) -> anArray Returns an array containing the names of public instance methods in the receiver. For a module, these are the public methods; for a class, they are the instance (not singleton) methods. With no argument, or with an argument that is false, the instance methods in mod are returned, otherwise the methods in mod and mod's superclasses are returned. module A def method1() end end class B def method2() end end class C < B def method3() end end A.instance_methods B.instance_methods C.instance_methods C.instance_methods(true).length
method_defined?
mod.method_defined?( aSymbol ) -> true or false
http://www.rubycentral.com/book/ref_c_module.html (4 of 10) [7/20/2001 9:49:35 PM]
» » » »
["method1"] ["method2"] ["method3"] 39
Programming Ruby: The Pragmatic Programmer's Guide
Returns true if the named method is defined by mod (or its included modules and, if mod is a class, its ancestors). module A def method1() end class B def method2() end class C < B include A def method3() end
end
end
A.method_defined? C.method_defined? C.method_defined? C.method_defined? C.method_defined?
end
module_eval
:method1 "method1" "method2" "method3" "method4"
» » » » »
true true true true false
mod.module_eval( aString ) -> anObject mod.module_eval {| | block } -> anObject Evaluates the string or block in the context of mod. This can be used to add methods to a class. module_eval returns the result of evaluating its argument. class Thing end a = %q{def hello() "Hello there!" end} » nil Thing.module_eval(a) » "Hello there!" Thing.new.hello()
name
mod.name -> aString Returns the name of the module mod.
private_class_method
mod.private_class_method( [aSymbol]+ ) -> nil Makes existing class methods private. Often used to hide the default constructor new. class SimpleSingleton # Not thread safe private_class_method :new def SimpleSingleton.create(*args, &block) @me = new(*args, &block) if ! @me @me end end
private_instance_methods
mod.private_instance_methods( includeSuper=false ) -> anArray
http://www.rubycentral.com/book/ref_c_module.html (5 of 10) [7/20/2001 9:49:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns a list of the private instance methods defined in mod. If the optional parameter is not false, the methods of any ancestors are included. module Mod def method1() end private :method1 def method2() end end Mod.instance_methods Mod.private_instance_methods
protected_instance_methods
» ["method2"] » ["method1"]
mod.protected_instance_methods( includeSuper=false ) -> anArray Returns a list of the protected instance methods defined in mod. If the optional parameter is not false, the methods of any ancestors are included.
public_class_method
mod.public_class_method( [aSymbol]+ ) -> nil Makes a list of existing class methods public.
public_instance_methods
mod.public_instance_methods( includeSuper=false ) -> anArray Returns a list of the public instance methods defined in mod. If the optional parameter is not false, the methods of any ancestors are included.
private methods alias_method
alias_method( newID, oldID ) -> mod Makes newID a new copy of the method oldID. This can be used to retain access to methods that are overridden. module Mod alias_method :origExit, :exit def exit(code=0) print "Exiting with code #{code}\n" origExit(code) end end include Mod exit(99) produces: Exiting with code 99
append_features
append_features( aModule ) -> mod When this module is included in another, Ruby calls append_features in this module, passing it the receiving module in aModule. Ruby's default implementation is to add the constants, methods, and module variables of this module to aModule if this module has not already been added to aModule or one of its ancestors. See also Module#include on page 350.
http://www.rubycentral.com/book/ref_c_module.html (6 of 10) [7/20/2001 9:49:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
attr
attr( aSymbol, writable=false ) -> nil Defines a named attribute for this module, where the name is aSymbol.id2name, creating an instance variable (@name) and a corresponding access method to read it. If the optional writable argument is true, also creates a method called name= to set the attribute. module Mod attr :size, true end is equivalent to: module Mod def size @size end def size=(val) @size = val end end
attr_accessor
attr_accessor( [aSymbol]+ ) -> nil Equivalent to calling ``attr aSymbol, true'' on each aSymbol in turn. module Mod attr_accessor(:one, :two) end Mod.instance_methods.sort » ["one", "one=", "two", "two="]
attr_reader
attr_reader( [aSymbol]+ ) -> nil Creates instance variables and corresponding methods that return the value of each instance variable. Equivalent to calling ``attr :name'' on each name in turn.
attr_writer
attr_writer( [aSymbol]+ ) -> nil Creates an accessor method to allow assignment to the attribute aSymbol.id2name.
extend_object
extend_object( anObject ) -> anObject
Extends the specified object by adding this module's constants and methods (which are added as singleton methods). This is the callback method used by Object#extend.
http://www.rubycentral.com/book/ref_c_module.html (7 of 10) [7/20/2001 9:49:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
module Picky def Picky.extend_object(o) if String === o print "Can't add Picky to a String\n" else print "Picky added to ", o.type, "\n" super end end end (s = Array.new).extend Picky # Call Object.extend (s = "quick brown fox").extend Picky produces: Picky added to Array Can't add Picky to a String
include
include( [aModule]+ ) -> mod Invokes Module.append_features (documented on page 349) on each parameter in turn.
method_added
method_added( aSymbol ) Invoked as a callback whenever a method is added to the receiver. module Chatty def Chatty.method_added(id) print "Adding ", id.id2name, "\n" end def one() end end module Chatty def two() end end produces: Adding one Adding two
module_function
module_function( [aSymbol]* ) -> mod Creates module functions for the named methods. These functions may be called with the module as a receiver, and also become available as instance methods to classes that mix in the module. Module functions are copies of the original, and so may be changed independently. The instance-method versions are made private. If used with no arguments, subsequently defined methods become module functions. module Mod def one "This is one" end module_function :one end class Cls
http://www.rubycentral.com/book/ref_c_module.html (8 of 10) [7/20/2001 9:49:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
include Mod def callOne one end end Mod.one c = Cls.new c.callOne module Mod def one "This is the end end Mod.one c.callOne
private
» "This is one" » "This is one"
new one"
» "This is one" » "This is the new one"
private( [aSymbol]* ) -> mod With no arguments, sets the default visibility for subsequently defined methods to private. With arguments, sets the named methods to have private visibility. See Access Control starting on page 235. module Mod def a() end def b() end private def c() end private :a end Mod.private_instance_methods
protected
» ["c", "a"]
protected( [aSymbol]* ) -> mod With no arguments, sets the default visibility for subsequently defined methods to protected. With arguments, sets the named methods to have protected visibility. See Access Control starting on page 235.
public
public( [aSymbol]* ) -> mod With no arguments, sets the default visibility for subsequently defined methods to public. With arguments, sets the named methods to have public visibility. See Access Control starting on page 235.
remove_const
remove_const( aSymbol ) -> anObject Removes the definition of the given constant, returning that constant's value. Predefined classes and singleton objects (such as true) cannot be removed.
remove_method
remove_method( aSymbol ) -> mod Removes the method identified by aSymbol from the current class. For an example, see Module.undef_method.
http://www.rubycentral.com/book/ref_c_module.html (9 of 10) [7/20/2001 9:49:35 PM]
Programming Ruby: The Pragmatic Programmer's Guide
undef_method
undef_method( aSymbol ) -> mod
Prevents the current class from responding to calls to the named method. Contrast this with remove_method, which deletes the method from the particular class; Ruby will still search superclasses and mixed-in modules for a possible receiver. class Parent def hello print "In parent\n" end end class Child < Parent def hello print "In child\n" end end c = Child.new c.hello class Child remove_method :hello end c.hello class Child undef_method :hello end c.hello
# remove from child, still in parent
# prevent any calls to 'hello'
produces: In child In parent prog.rb:23: undefined method `hello' for # (NameError)
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Module Version: 1.6
Index: inherited new new superclass Classes in Ruby are first-class objects---each is an instance of class Class. When a new class is created (typically using class Name ... end), an object of type Class is created and assigned to a global constant (Name in this case). When Name.new is called to create a new object, the new method in Class is run by default. This can be demonstrated by overriding new in Class: class Class alias oldNew new def new(*args) print "Creating a new ", self.name, "\n" oldNew(*args) end end class Name end n = Name.new produces: Creating a new Name
class methods
http://www.rubycentral.com/book/ref_c_class.html (1 of 3) [7/20/2001 9:49:36 PM]
Programming Ruby: The Pragmatic Programmer's Guide
inherited
aClass.inherited( aSubClass )
This is a singleton method (per class) invoked by Ruby when a subclass of aClass is created. The new subclass is passed as a parameter. class Top def Top.inherited(sub) print "New subclass: ", sub, "\n" end end class Middle < Top end class Bottom < Middle end produces: New subclass: Middle New subclass: Bottom
new
Class.new( aSuperClass=Object ) -> aClass Creates a new anonymous (unnamed) class with the given superclass (or Object if no parameter is given).
instance methods new
aClass.new( [args]* ) -> anObject Creates a new object of aClass's class, then invokes that object's initialize method, passing it args.
superclass
aClass.superclass -> aSuperClass or nil Returns the superclass of aClass, or nil. Class.superclass Object.superclass
http://www.rubycentral.com/book/ref_c_class.html (2 of 3) [7/20/2001 9:49:36 PM]
» »
Module nil
Programming Ruby: The Pragmatic Programmer's Guide
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Locking Ruby in the Safe Walter Webcoder has a great idea for a portal site: The Web Arithmetic Page. Surrounded by all sorts of cool mathematical links and banner ads that will make him rich is a simple central frame, containing a text field and a button. Users type an arithmetic expression into the field, press the button, and the answer is displayed. All the world's calculators become obsolete overnight, and Walter cashes in and retires to devote his life to his collection of car license plate numbers. Implementing the calculator is easy, thinks Walter. He accesses the contents of the form field using Ruby's CGI library, and uses the eval method to evaluate the string as an expression. require 'cgi' cgi = CGI::new("html4") # Fetch the value of the form field "expression" expr = cgi["expression"].to_s begin result = eval(expr) rescue Exception => detail # handle bad expressions end # display result back to user... Roughly seven seconds after Walter puts the application online, a twelve-year-old from Waxahachie with glandular problems and no real life types ``system("rm *")'' into the form and, like his application, Walter's dreams come tumbling down. Walter learned an important lesson: All external data is dangerous. Don't let it close to interfaces that can modify your system. In this case, the content of the form field was the external data, and the call to eval was the security breach. Fortunately, Ruby provides support for reducing this risk. All information from the outside world can be marked as tainted. When running in a safe mode, potentially dangerous methods will raise a SecurityError if passed a tainted object.
Safe Levels The variable $SAFE determines Ruby's level of paranoia. Table 20.1 on page 261 gives details of the checks performed at each safe level. $SAFE Constraints http://www.rubycentral.com/book/taint.html (1 of 4) [7/20/2001 9:49:37 PM]
Programming Ruby: The Pragmatic Programmer's Guide
0 >= 1 >= 2 >= 3 >= 4
No checking of the use of externally supplied (tainted) data is performed. This is Ruby's default mode. Ruby disallows the use of tainted data by potentially dangerous operations. Ruby prohibits the loading of program files from globally writable locations. All newly created objects are considered tainted. Ruby effectively partitions the running program in two. Nontainted objects may not be modified. Typically, this will be used to create a sandbox: the program sets up an environment using a lower $SAFE level, then resets $SAFE to 4 to prevent subsequent changes to that environment.
The default value of $SAFE is zero under most circumstances. However, if a Ruby script is run setuid or setgid,[A Unix script may be flagged to be run under a different user or group id than the person running it. This allows the script to have privileges that the user does not have; the script can access resources that the user would otherwise be prohibited from using. These scripts are called setuid or setgid.] its safe level is automatically set to 1. The safe level may also be set using the -T command-line option, and by assigning to $SAFE within the program. It is not possible to lower the value of $SAFE by assignment. The current value of $SAFE is inherited when new threads are created. However, within each thread, the value of $SAFE may be changed without affecting the value in other threads. This facility may be used to implement secure ``sandboxes,'' areas where external code may run safely without risking the rest of your application or system. Do this by wrapping code that you load from a file in its own, anonymous module. This will protect your program's namespace from any unintended alteration. f=open(fileName,"w") f.print ... # write untrusted program into file. f.close Thread.start { $SAFE = 4 load(fileName, true) } With a $SAFE level of 4, you can load only wrapped files. See Kernel::load on page 422 for details.
Tainted Objects Any Ruby object derived from some external source (for example, a string read from a file, or an environment variable) is automatically marked as being tainted. If your program uses a tainted object to derive a new object, then that new object will also be tainted, as shown in the code below. Any object with external data somewhere in its past will be tainted. This tainting process is performed regardless of the current safe level. You can inspect the tainted status of an object using Object#tainted?. # internal data # ============= x1 = "a string" x1.tainted?
»
false
# external data # ============= y1 = ENV["HOME"] y1.tainted?
»
true
»
true
» »
1 true
x2 = x1[2, 4] x2.tainted?
»
false
y2 = y1[2, 4] y2.tainted?
x1 =~ /([a-z])/ $1.tainted?
» »
0 false
y1 =~ /([a-z])/ $1.tainted?
You can force any object to become tainted by invoking its taint method. If the safe level is less than 3, you can remove the taint from an object by invoking untaint.[There are also some devious ways of doing this without using untaint. We'll leave it up to your darker side to find them.] This is clearly not something to do lightly. Clearly, Walter should have run his CGI script at a safe level of 1. This would have raised an exception when the program tried to pass form data to eval. Once this had happened, Walter would have had a number of choices. He could have chosen to implement a proper expression parser, bypassing the risks inherent in using eval. However, being lazy, it's more likely he'd have performed some simple sanity check on the form data, and untaint it if it looked innocuous. http://www.rubycentral.com/book/taint.html (2 of 4) [7/20/2001 9:49:37 PM]
Programming Ruby: The Pragmatic Programmer's Guide
require 'cgi'; $SAFE = 1 cgi = CGI::new("html4") expr = cgi["field"].to_s if expr =~ %r{^-+*/\d\seE.()*$} expr.untaint result = eval(expr) # display result back to user... else # display error message... end Personally, we think Walter's still taking undue risks. We'd probably prefer to see a real parser here, but implementing one here has nothing to teach us about tainting, so we'll move on. And remember---it's a dangerous world out there. Be careful. Definition of the safe levels $SAFE >= 1 ❍ The environment variables RUBYLIB and RUBYOPT are not processed, and the current directory is not added to the path. ❍ The command-line options -e, -i, -I, -r, -s, -S, and -x are not allowed. ❍ Can't start processes from $PATH if any directory in it is world-writable. ❍ Can't manipulate or chroot to a directory whose name is a tainted string. ❍ Can't glob tainted strings. ❍ Can't eval tainted strings. ❍ Can't load or require a file whose name is a tainted string. ❍ Can't manipulate or query the status of a file or pipe whose name is a tainted string. ❍ Can't execute a system command or exec a program from a tainted string. ❍ Can't pass trap a tainted string. $SAFE >= 2 ❍ Can't change, make, or remove directories, or use chroot. ❍ Can't load a file from a world-writable directory. ❍ Can't load a file from a tainted filename starting with ~. ❍ Can't use File#chmod, File#chown, File#lstat, File::stat, File#truncate, File::umask, File#flock, IO#ioctl, IO#stat, Kernel#fork, Kernel#syscall, Kernel#trap. Process::setpgid, Process::setsid, http://www.rubycentral.com/book/taint.html (3 of 4) [7/20/2001 9:49:37 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Process::setpriority, or Process::egid=. ❍
Can't handle signals using trap.
$SAFE >= 3 ❍ ❍
All objects are created tainted. Can't untaint objects.
$SAFE >= 4 ❍ ❍ ❍ ❍ ❍ ❍ ❍ ❍ ❍ ❍
❍ ❍ ❍
❍ ❍ ❍ ❍ ❍
❍ ❍ ❍ ❍
Can't modify a nontainted array, hash, or string. Can't modify a global variable. Can't access instance variables of nontainted objects. Can't change an environment variable. Can't close or reopen nontainted files. Can't freeze nontainted objects. Can't change visibility of methods (private/public/protected). Can't make an alias in a nontainted class or module. Can't get meta information (such as method or variable lists). Can't define, redefine, remove, or undef a method in a nontainted class or module. Can't modify Object. Can't remove instance variables or constants from nontainted objects. Can't manipulate threads, terminate a thread other than the current, or set abort_on_exception. Can't have thread local variables. Can't raise an exception in a thread with a lower $SAFE value. Can't move threads between ThreadGroups. Can't invoke exit, exit!, or abort. Can load only wrapped files, and can't include modules in nontainted classes and modules. Can't convert symbol identifiers to object references. Can't write to files or pipes. Can't use autoload. Can't taint objects.
Previous < Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide" Copyright © 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0. This reference is available for download.
http://www.rubycentral.com/book/taint.html (4 of 4) [7/20/2001 9:49:37 PM]
Contents ^
Next >
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
module ObjectSpace Index: _id2ref define_finalizer each_object garbage_collect undefine_finalizer The ObjectSpace module contains a number of routines that interact with the garbage collection facility and allow you to traverse all living objects with an iterator. ObjectSpace also provides support for object finalizers, procs that will be called when a specific object is about to be destroyed by garbage collection. include ObjectSpace a = "A" b = "B" c = "C" define_finalizer(a, proc {|id| puts "Finalizer one on #{id}" }) define_finalizer(a, proc {|id| puts "Finalizer two on #{id}" }) define_finalizer(b, proc {|id| puts "Finalizer three on #{id}" }) produces: 0x4018d4f0 n finals=>1 Finalizer three on 537684600 0x4018d504 n finals=>0 Finalizer one on 537684610 n finals=>0 Finalizer two on 537684610
class methods _id2ref
ObjectSpace._id2ref( anId ) -> anObject
http://www.rubycentral.com/book/ref_m_objectspace.html (1 of 2) [7/20/2001 9:49:37 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Converts an object id to a reference to the object. May not be called on an object id passed as a parameter to a finalizer. s = "I am a string" r = ObjectSpace._id2ref(s.id) r == s
define_finalizer
» "I am a string" » "I am a string" » true
ObjectSpace.define_finalizer( anObject, aProc=proc() ) Adds aProc as a finalizer, to be called when anObject is about to be destroyed.
each_object
ObjectSpace.each_object( [ aClassOrMod] ) {| anObj | block } -> aFixnum Calls the block once for each living, nonimmediate object in this Ruby process. If aClassOrMod is specified, calls the block for only those classes or modules that match (or are a subclass of) aClassOrMod. Returns the number of objects found. a = 102.7 b = 95 ObjectSpace.each_object(Numeric) {|x| p x } print "Total count: ", ObjectSpace.each_object {} ,"\n" produces: 102.7 2.718281828 3.141592654 Total count: 376
garbage_collect
ObjectSpace.garbage_collect -> nil Initiates garbage collection (see module GC on page 414).
undefine_finalizer
ObjectSpace.undefine_finalizer( anObject ) Removes all finalizers for anObject.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Reflection, ObjectSpace, and Distributed Ruby One of the many advantages of dynamic languages such as Ruby is the ability to introspect---to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection. The word ``reflection'' conjures up an image of looking at oneself in the mirror---perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand. In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover: ● what objects it contains, ● the current class hierarchy, ● the contents and behaviors of objects, and ● information on methods. Armed with this information, we can look at particular objects and decide which of their methods to call at runtime---even if the class of the object didn't exist when we first wrote the code. We can also start doing clever things, perhaps modifying the program as it's running. Sound scary? It needn't be. In fact, these reflection capabilities let us do some very useful things. Later in this chapter we'll look at distributed Ruby and marshaling, two reflection-based technologies that let us send objects around the world and through time.
Looking at Objects Have you ever craved the ability to traverse all the living objects in your program? We have! Ruby lets you perform this trick with ObjectSpace::each_object. We can use it to do all sorts of neat tricks. For example, to iterate over all objects of type Numeric, you'd write the following.
http://www.rubycentral.com/book/ospace.html (1 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
a = 102.7 b = 95.1 ObjectSpace.each_object(Numeric) {|x| p x } produces: 95.1 102.7 2.718281828 3.141592654 Hey, where did those last two numbers come from? We didn't define them in our program. If you look on page 433, you'll see that the Math module defines constants for e and PI; since we are examining all living objects in the system, these turn up as well. However, there is a catch. Let's try the same example with different numbers. a = 102 b = 95 ObjectSpace.each_object(Numeric) {|x| p x } produces: 2.718281828 3.141592654 Neither of the Fixnum objects we created showed up. That's because ObjectSpace doesn't know about objects with immediate values: Fixnum, true, false, and nil.
Looking Inside Objects Once you've found an interesting object, you may be tempted to find out just what it can do. Unlike static languages, where a variable's type determines its class, and hence the methods it supports, Ruby supports liberated objects. You really cannot tell exactly what an object can do until you look under its hood.[Or under its bonnet, for objects created to the east of the Atlantic.] For instance, we can get a list of all the methods to which an object will respond. r = 1..10 # Create a Range object list = r.methods list.length » 60 list[0..3] » ["size", "length", "exclude_end?", "inspect"] Or, we can check to see if an object supports a particular method. r.respond_to?("frozen?") r.respond_to?("hasKey")
http://www.rubycentral.com/book/ospace.html (2 of 15) [7/20/2001 9:49:39 PM]
» true » false
Programming Ruby: The Pragmatic Programmer's Guide
» true
"me".respond_to?("==")
We can determine our object's class and its unique object id, and test its relationship to other classes. num = 1 num.id num.class num.kind_of? Fixnum num.kind_of? Numeric num.instance_of? Fixnum num.instance_of? Numeric
» » » » » »
3 Fixnum true true true false
Looking at Classes Knowing about objects is one part of reflection, but to get the whole picture, you also need to be able to look at classes---the methods and constants that they contain. Looking at the class hierarchy is easy. You can get the parent of any particular class using Class#superclass. For classes and modules, Module#ancestors lists both superclasses and mixed-in modules. klass = Fixnum begin print klass klass = klass.superclass print " < " if klass end while klass puts p Fixnum.ancestors produces: Fixnum < Integer < Numeric < Object [Fixnum, Integer, Precision, Numeric, Comparable, Object, Kernel] If you want to build a complete class hierarchy, just run that code for every class in the system. We can use ObjectSpace to iterate over all Class objects: ObjectSpace.each_object(Class) do |aClass| # ... end
http://www.rubycentral.com/book/ospace.html (3 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Looking Inside Classes We can find out a bit more about the methods and constants in a particular object. Instead of just checking to see whether the object responds to a given message, we can ask for methods by access level, we can ask for just singleton methods, and we can have a look at the object's constants. class Demo private def privMethod end protected def protMethod end public def pubMethod end def Demo.classMethod end CONST = 1.23 end Demo.private_instance_methods Demo.protected_instance_methods Demo.public_instance_methods Demo.singleton_methods Demo.constants - Demo.superclass.constants
» » » » »
["privMethod"] ["protMethod"] ["pubMethod"] ["classMethod"] ["CONST"]
Module::constants returns all the constants available via a module, including constants from the module's superclasses. We're not interested in those just at the moment, so we'll subtract them from our list. Given a list of method names, we might now be tempted to try calling them. Fortunately, that's easy with Ruby.
Calling Methods Dynamically C and Java programmers often find themselves writing some kind of dispatch table: functions which are invoked based on a command. Think of a typical C idiom where you have to translate a string to a function pointer:
http://www.rubycentral.com/book/ospace.html (4 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
typedef struct { char *name; void (*fptr)(); } Tuple; Tuple list[]= { "play", { "stop", { "record", { 0, 0 }, };
{ fptr_play }, fptr_stop }, fptr_record },
... void dispatch(char *cmd) { int i = 0; for (; list[i].name; i++) { if (strncmp(list[i].name,cmd,strlen(cmd)) == 0) { list[i].fptr(); return; } } /* not found */ } In Ruby, you can do all this in one line. Stick all your command functions into a class, create an instance of that class (we called it commands), and ask that object to execute a method called the same name as the command string. commands.send(commandString) Oh, and by the way, it does much more than the C version---it's dynamic. The Ruby version will find new methods added at runtime just as easily. You don't have to write special command classes for send: it works on any object. "John Coltrane".send(:length) "Miles Davis".send("sub", /iles/, '.')
» 13 » "M. Davis"
Another way of invoking methods dynamically uses Method objects. A Method object is like a Proc object: it represents a chunk of code and a context in which it executes. In this case, the code is the body of the method, and the context is the object that created the method. Once we have our Method object, we can execute it sometime later by sending it the message call. trane = "John Coltrane".method(:length) miles = "Miles Davis".method("sub")
http://www.rubycentral.com/book/ospace.html (5 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
» 13 » "M. Davis"
trane.call miles.call(/iles/, '.')
You can pass the Method object around as you would any other object, and when you invoke Method#call, the method is run just as if you had invoked it on the original object. It's like having a C-style function pointer but in a fully object-oriented style. You can also use Method objects with iterators. def double(a) 2*a end mObj = method(:double) » [2, 6, 10, 14]
[ 1, 3, 5, 7 ].collect(&mObj)
As good things come in threes, here's yet another way to invoke methods dynamically. The eval method (and its variations such as class_eval, module_eval, and instance_eval) will parse and execute an arbitrary string of legal Ruby source code. trane = %q{"John Coltrane".length} miles = %q{"Miles Davis".sub(/iles/, '.')} eval trane eval miles
» »
13 "M. Davis"
When using eval, it can be helpful to state explicitly the context in which the expression should be evaluated, rather than using the current context. You can obtain a context by calling Kernel#binding at the desired point. class CoinSlot def initialize(amt=Cents.new(25)) @amt = amt $here = binding end end a = CoinSlot.new eval "puts @amt", $here eval "puts @amt" produces: $0.25USD nil The first eval evaluates @amt in the context of the instance of class CoinSlot. The second eval
http://www.rubycentral.com/book/ospace.html (6 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
evaluates @amt in the context of Object, where the instance variable @amt is not defined.
Performance Considerations As we've seen in this section, there are several ways to invoke an arbitrary method of some object: Object#send, Method#call, and the various flavors of eval. You may prefer to use any one of these techniques depending on your needs, but be aware that eval is significantly slower than the others (or, for optimistic readers, send and call are significantly faster than eval). require "benchmark" include Benchmark
# from the Ruby Application Archive
test = "Stormy Weather" m = test.method(:length) n = 100000 bm(12) {|x| x.report("call") { n.times { m.call } } x.report("send") { n.times { test.send(:length) } } x.report("eval") { n.times { eval "test.length" } } } produces:
call send eval
user 0.270000 0.250000 2.990000
system 0.000000 0.000000 0.050000
total 0.270000 ( 0.250000 ( 3.040000 (
real 0.243899) 0.227987) 2.929909)
System Hooks A hook is a technique that lets you trap some Ruby event, such as object creation. The simplest hook technique in Ruby is to intercept calls to methods in system classes. Perhaps you want to log all the operating system commands your program executes. Simply rename the method Kernel::system[This Eiffel-inspired idiom of renaming a feature and redefining a new one is very useful, but be aware that it can cause problems. If a subclass does the same thing, and renames the methods using the same names, you'll end up with an infinite loop. You can avoid this by aliasing your methods to a unique symbol name or by using a consistent naming convention.] and substitute it with one of your own that both logs the command and calls the original Kernel method.
http://www.rubycentral.com/book/ospace.html (7 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
module Kernel alias_method :old_system, :system def system(*args) result = old_system(*args) puts "system(#{args.join(', ')}) returned #{result}" result end end system("date") system("kangaroo", "-hop 10", "skippy") produces: Sun Mar 4 23:25:46 CST 2001 system(date) returned true system(kangaroo, -hop 10, skippy) returned false A more powerful hook is catching objects as they are created. If you can be present when every object is born, you can do all sorts of interesting things: you can wrap them, add methods to them, remove methods from them, add them to containers to implement persistence, you name it. We'll show a simple example here: we'll add a timestamp to every object as it's created. One way to hook object creation is to do our method renaming trick on Class#new, the method that's called to allocate space for a new object. The technique isn't perfect---some built-in objects, such as literal strings, are constructed without calling new---but it'll work just fine for objects we write. class Class alias_method :old_new, :new def new(*args) result = old_new(*args) result.timestamp = Time.now return result end end We'll also need to add a timestamp attribute to every object in the system. We can do this by hacking class Object itself.
http://www.rubycentral.com/book/ospace.html (8 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class Object def timestamp return @timestamp end def timestamp=(aTime) @timestamp = aTime end end Finally, we can run a test. We'll create a couple of objects a few seconds apart and check their timestamps. class Test end obj1 = Test.new sleep 2 obj2 = Test.new obj1.timestamp obj2.timestamp
» Sun Mar 04 23:25:46 CST 2001 » Sun Mar 04 23:25:48 CST 2001
All this method renaming is fine, and it really does work. However, there are other, more refined ways to get inside a running program. Ruby provides several callback methods that let you trap certain events in a controlled way.
Runtime Callbacks You can be notified whenever one of the following events occurs: Event Callback Method Adding an instance method Module#method_added Adding a singleton method Kernel::singleton_method_added Subclassing a class
Class#inherited
Mixing in a module
Module#extend_object
These techniques are all illustrated in the library descriptions for each callback method. At runtime, these methods will be called by the system when the specified event occurs. By default, these methods do nothing. If you want to be notified when one of these events happens, just define the callback method, and you're in. Keeping track of method creation and class and module usage lets you build an accurate picture of the dynamic state of your program. This can be important. For example, you may have written code that wraps all the methods in a class, perhaps to add transactional support or to implement some form of delegation. This is only half the job: the dynamic nature of Ruby means that users of this class could add new methods to it at any time. Using these callbacks, you can write code that wraps these new methods http://www.rubycentral.com/book/ospace.html (9 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
as they are created.
Tracing Your Program's Execution While we're having fun reflecting on all the objects and classes in our programs, let's not forget about the humble statements that make our code actually do things. It turns out that Ruby lets us look at these statements, too. First, you can watch the interpreter as it executes code. set_trace_func executes a Proc with all sorts of juicy debugging information whenever a new source line is executed, methods are called, objects are created, and so on. There's a full description on page 426, but here's a taste. class def a b end end
Test test = 1 = 2
set_trace_func proc { |event, file, line, id, binding, classname| printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname } t = Test.new t.test produces: line c-call c-call c-return c-return line call line line return
prog.rb:11 prog.rb:11 new prog.rb:11 initialize prog.rb:11 initialize prog.rb:11 new prog.rb:12 prog.rb:2 test prog.rb:3 test prog.rb:4 test prog.rb:4 test
false Class Object Object Class false Test Test Test Test
There's also a method trace_var (described on page 431) that lets you add a hook to a global variable; whenever an assignment is made to the global, your Proc object is invoked.
How Did We Get Here? A fair question, and one we ask ourselves regularly. Mental lapses aside, in Ruby at least you can find out exactly ``how you got there'' by using the method caller, which returns an Array of String objects representing the current call stack. http://www.rubycentral.com/book/ospace.html (10 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
def catA puts caller.join("\n") end def catB catA end def catC catB end catC produces: prog.rb:5:in `catB' prog.rb:8:in `catC' prog.rb:10 Once you've figured out how you got there, where you go next is up to you.
Marshaling and Distributed Ruby Java features the ability to serialize objects, letting you store them somewhere and reconstitute them when needed. You might use this facility, for instance, to save a tree of objects that represent some portion of application state---a document, a CAD drawing, a piece of music, and so on. Ruby calls this kind of serialization marshaling.[Think of railroad marshaling yards where individual cars are assembled in sequence into a complete train, which is then dispatched somewhere.] Saving an object and some or all of its components is done using the method Marshal::dump. Typically, you will dump an entire object tree starting with some given object. Later on, you can reconstitute the object using Marshal::load. Here's a short example. We have a class Chord that holds a collection of musical notes. We'd like to save away a particularly wonderful chord so our grandchildren can load it into Ruby Version 23.5 and savor it, too. Let's start off with the classes for Note and Chord.
http://www.rubycentral.com/book/ospace.html (11 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class Note attr :value def initialize(val) @value = val end def to_s @value.to_s end end class Chord def initialize(arr) @arr = arr end def play @arr.join('-') end end Now we'll create our masterpiece, and use Marshal::dump to save a serialized version of it to disk. c = Chord.new( [ Note.new("G"), Note.new("Bb"), Note.new("Db"), Note.new("E") ] ) File.open("posterity", "w+") do |f| Marshal.dump(c, f) end Finally, our grandchildren read it in, and are transported by our creation's beauty. File.open("posterity") do |f| chord = Marshal.load(f) end chord.play
»
"G-Bb-Db-E"
Custom Serialization Strategy Not all objects can be dumped: bindings, procedure objects, instances of class IO, and singleton objects cannot be saved outside of the running Ruby environment (a TypeError will be raised if you try). Even if your object doesn't contain one of these problematic objects, you may want to take control of object serialization yourself. Marshal provides the hooks you need. In the objects that require custom serialization, simply implement two methods: an instance method called _dump, which writes the object out to a string, and a
http://www.rubycentral.com/book/ospace.html (12 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class method called _load, which reads a string that you'd previously created and converts it into a new object. For instance, here is a sample class that defines its own serialization. For whatever reasons, Special doesn't want to save one of its internal data members, ``@volatile''. class Special def initialize(valuable) @valuable = valuable @volatile = "Goodbye" end def _dump(depth) @valuable.to_str end def Special._load(str) result = Special.new(str); end def to_s "#{@valuable} and #{@volatile}" end end a = Special.new("Hello, World") data = Marshal.dump(a) obj = Marshal.load(data) puts obj produces: Hello, World and Goodbye For more details, see the reference section on Marshal beginning on page 432.
Distributed Ruby Since we can serialize an object or a set of objects into a form suitable for out-of-process storage, we can use this capability for the transmission of objects from one process to another. Couple this capability with the power of networking, and voilà: you have a distributed object system. To save you the trouble of having to write the code, we suggest downloading Masatoshi Seki's Distributed Ruby library (drb) from the RAA. Using drb, a Ruby process may act as a server, as a client, or as both. A drb server acts as a source of objects, while a client is a user of those objects. To the client, it appears that the objects are local, but in
http://www.rubycentral.com/book/ospace.html (13 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
reality the code is still being executed remotely. A server starts a service by associating an object with a given port. Threads are created internally to handle incoming requests on that port, so remember to join the drb thread before exiting your program. require 'drb' class TestServer def doit "Hello, Distributed World" end end aServerObject = TestServer.new DRb.start_service('druby://localhost:9000', aServerObject) DRb.thread.join # Don't exit just yet! A simple drb client simply creates a local drb object and associates it with the object on the remote server; the local object is a proxy. require 'drb' DRb.start_service() obj = DRbObject.new(nil, 'druby://localhost:9000') # Now use obj p obj.doit The client connects to the server and calls the method doit, which returns a string that the client prints out: "Hello, Distributed World" The initial nil argument to DRbObject indicates that we want to attach to a new distributed object. We could also use an existing object. Ho hum, you say. This sounds like Java's RMI, or CORBA, or whatever. Yes, it is a functional distributed object mechanism---but it is written in just 200 lines of Ruby code. No C, nothing fancy, just plain old Ruby code. Of course, there's no naming service or trader service, or anything like you'd see in CORBA, but it is simple and reasonably fast. On the 233MHz test system, this sample code runs at about 50 remote message calls per second. And, if you like the look of Sun's JavaSpaces, the basis of their JINI architecture, you'll be interested to know that drb is distributed with a short module that does the same kind of thing. JavaSpaces is based on a technology called Linda. To prove that its Japanese author has a sense of humor, Ruby's version of Linda is known as ``rinda.''
http://www.rubycentral.com/book/ospace.html (14 of 15) [7/20/2001 9:49:39 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Compile Time? Runtime? Anytime! The important thing to remember about Ruby is that there isn't a big difference between ``compile time'' and ``runtime.'' It's all the same. You can add code to a running process. You can redefine methods on the fly, change their scope from public to private, and so on. You can even alter basic types, such as Class and Object. Once you get used to this flexibility, it is hard to go back to a static language such as C++, or even to a half-static language such as Java. But then, why would you want to? Previous
Ruby Class and Library Reference
Programming Ruby
Contents ^ Next > Previous
anObject Invokes the meth with the specified arguments, returning the method's return value. m = 12.method("+") m.call(3) m.call(20)
to_proc
» »
15 32
meth.to_proc -> aProc Returns a Proc object corresponding to this method.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
module Marshal Index: dump load restore The marshaling library converts collections of Ruby objects into a byte stream, allowing them to be stored outside the currently active script. This data may subsequently be read and the original objects reconstituted. Marshaling is described starting on page 272. Some objects cannot be dumped: if the objects to be dumped include bindings, procedure objects, instances of class IO, or singleton objects, a TypeError will be raised. If your class has special serialization needs (for example, if you want to serialize in some specific format), or if it contains objects that would otherwise not be serializable, you can implement your own serialization strategy by defining two methods, _dump and _load: Method Type Signature Returns Instance _dump(aDepth) Returns a String Class _load(aString) Returns a reconstituted Object The instance method _dump should return a String object containing all the information necessary to reconstitute objects of this class and all referenced objects up to a maximum depth of aDepth (a value of -1 should disable depth checking). The class method _load should take a String and return an object of this class.
class methods dump
dump( anObject [, anIO] , limit=--1 ) -> anIO
http://www.rubycentral.com/book/ref_m_marshal.html (1 of 2) [7/20/2001 9:49:43 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Serializes anObject and all descendent objects. If anIO is specified, the serialized data will be written to it, otherwise the data will be returned as a String. If limit is specified, the traversal of subobjects will be limited to that depth. If limit is negative, no checking of depth will be performed. class Klass def initialize(str) @str = str end def sayHello @str end end o = Klass.new("hello\n") data = Marshal.dump(o) obj = Marshal.load(data) obj.sayHello
load
»
"hello\n"
load( from [, aProc] ) -> anObject Returns the result of converting the serialized data in from into a Ruby object (possibly with associated subordinate objects). from may be either an instance of IO or an object that responds to to_str. If proc is specified, it will be passed each object as it is deserialized.
restore
restore( from [, aProc] ) -> anObject A synonym for Marshal::load.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Standard Library Ruby comes ``out of the box'' with a large and useful library of modules and classes. This chapter contains a sampling of the more useful of these. Interestingly, and unlike some of the code in later chapters, all of these libraries are written in Ruby. You'll find the source in the lib subdirectory of the standard Ruby distribution.
class Complex
Parent: Numeric Version: 1.6
Index: new Arithmetic operations == abs abs2 arg conjugate image polar real to_f to_i to_r to_s require "complex" v1 = Complex(2,3) v2 = 2.im v1 + v2 v1 * v2 v2**2 Math.sin(v1) v1 < v2 v2**2 == -4
» » » » » » » »
Complex(2, 3) Complex(0, 2) Complex(2, 5) Complex(-6, 4) Complex(-4, 0) Complex(9.154499147, -4.16890696) false true
constants Complex::I
0 +1i
class methods new
Complex.new( a, b ) -> aComplex
http://www.rubycentral.com/book/lib_standard.html (1 of 26) [7/20/2001 9:49:47 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns a + bi. In addition to the Complex#new constructor, the Complex library defines the method Numeric::im, such that aNumeric.im returns 0 + aNumerici. Complex numbers are also constructed using the global method Complex, which takes one or two arguments. The value it returns depends on the type of its arguments: a b Result Number Number a + bi Complex 0 a Complex Complex Complex( a.real - b.image, a.image + b.real ) Number Complex Complex( a - b.image, b.real )
instance methods Arithmetic operations Performs various arithmetic operations on ref. ref ref ref ref ref ref
+ * / % **
aNumeric -> aComplex aNumeric -> aComplex aNumeric -> aComplex aNumeric -> aComplex aNumeric -> aComplex aNumeric -> aComplex
Addition Subtraction Multiplication Division Remainder Exponentiation (real and complex power)
ref other -> -1, 0, +1 Returns ref.abs other.abs.
==
ref == anObject -> true or false If anObject is a complex number, returns true if its real and imaginary parts match ref. If anObject is a simple number, returns true if ref.real equals anObject and ref.image is zero. Otherwise, attempts to coerce anObject to a complex number and compares the result.
abs
ref.abs -> aFloat Absolute value.
abs2
ref.abs2 -> aFloat Square of absolute value.
http://www.rubycentral.com/book/lib_standard.html (2 of 26) [7/20/2001 9:49:47 PM]
Programming Ruby: The Pragmatic Programmer's Guide
arg
ref.arg -> aFloat Argument (angle from (1,0)).
conjugate
ref.conjugate -> aComplex Complex conjugate.
image
ref.image -> aNumeric The imaginary part of ref.
polar
ref.polar -> anArray Returns the two-element array: [c.abs, c.arg].
real
ref.real -> aNumeric The real part of ref.
to_f
ref.to_f -> aComplex Returns Complex(real.to_f, image.to_f).
to_i
ref.to_i -> aComplex Returns Complex(real.to_i, image.to_i).
to_r
ref.to_r -> aComplex Returns Complex(real.to_r, image.to_r), converting both parts of the complex to a rational number.
to_s
ref.to_s -> aString String representation of ref.
In addition, the Math functions sqrt, exp, cos, sin, tan, log, log10, and atan2 are extended to support a Complex argument.
class Date
Parent: Object Version: 1.6
http://www.rubycentral.com/book/lib_standard.html (3 of 26) [7/20/2001 9:49:47 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Index: exist2? exist? existw? gregorian_leap? julian_leap? leap? new new1 new2 new3 neww today Accessors + -- > downto england gregorian italy jd julian leap? mjd newsg next ns? os? sg step succ to_s upto require 'date' d = Date.new(2000, 3, 31) [d.year, d.yday, d.wday] [d.month, d.mday] [d.cwyear, d.cweek, d.cwday] [d.jd, d.mjd] (d true or false Synonym for Date#gregorian_leap?.
new
Date.new( year=-4712, mon=1, mday=1, sg=Date::ITALY) -> aNewDate Returns a Date for the given year, mon, and mday. If mon is negative, it counts back from the end of the year. If mday is negative, it counts back from the end of the month.
new1
Date.new1( jd, sg=Date::ITALY) -> aNewDate Creates a Date corresponding to the given Julian day number.
new2
Date.new2( year=-4712, yday=1, sg=Date::ITALY) -> aNewDate Returns a Date for the given year and yday. If yday is negative, it counts back from the end of the year.
new3
Date.new3( year=-4712, mon=1, mday=1, sg=Date::ITALY) -> aNewDate Synonym for Date#new.
neww
Date.neww( cyear=1582, cweek=41, cwday=5, sg=Date::ITALY) -> aNewDate Returns a Date for the given cyear, cweek, and cwday. If cweek is negative, it counts back from the end of the year. If cwday is negative, it counts back from the end of the week.
today
Date.today( sg=Date::ITALY) -> aNewDate Returns a Date for today.
instance methods
http://www.rubycentral.com/book/lib_standard.html (6 of 26) [7/20/2001 9:49:47 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Accessors
ref.year -> year ref.yday -> yday ref.mjd -> mjd ref.mon -> mon ref.month -> mon ref.mday -> mday ref.day -> mday ref.cwyear -> cwyear ref.cweek -> cweek ref.cwday -> cwday ref.wday -> wday Returns the given component of ref as a number.
+
ref + anInteger -> aNewDate Returns a new Date anInteger days from ref.
--
ref - anInteger -> aNewDate ref - anOtherDate -> anInteger The first form returns a new Date anInteger days before ref. The second form returns the number of days between ref and anOtherDate.
true or false anOther must be a Numeric, in which case it is treated as a Julian day number, or a Date. Returns true if the Julian day number of anOther is the same as ref.
>>
ref >> anInteger -> aNewDate Returns a new Date formed by adding anInteger months to ref, adjusting the mday value back to the last day of the month if it otherwise exceeds it.
downto
ref.downto( aDateMin ) {| date | block } -> ref Invokes block with dates from ref down to aDateMin.
http://www.rubycentral.com/book/lib_standard.html (7 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
england
ref.england -> aDate Equivalent to ref.newsg(Date::ENGLAND).
gregorian
ref.gregorian -> aDate Equivalent to ref.newsg(Date::GREGORIAN).
italy
ref.italy -> aDate Equivalent to ref.newsg(Date::ITALY).
jd
ref.jd -> jd Returns the Julian day number for ref.
julian
ref.julian -> aDate Equivalent to ref.newsg(Date::JULIAN).
leap?
ref.leap? -> true or false Returns true if ref falls within a leap year.
mjd
ref.mjd -> mjd Returns the Julian day number of ref converted to a modified Julian day number.
newsg
ref.newsg( sg=Date::ITALY ) -> aNewDate Returns a new Date.
next
ref.next -> aNewDate Synonym for ref.succ.
ns?
ref.ns? -> true or false Returns true if ref falls in the period of New Style dates.
os?
ref.os? -> true or false Returns true if ref falls in the period of Old Style dates.
sg
ref.sg -> anInteger Returns the Julian day number of the start of New Style dates for ref.
http://www.rubycentral.com/book/lib_standard.html (8 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
step
ref.step( aDateLimit, step ) {| date | block } -> ref Invokes block with dates starting at ref, incrementing by step days, ending at the first date greater than aDateLimit (less than for a negative step).
succ
ref.succ -> aNewDate Returns the date of ref plus one day.
to_s
ref.to_s -> aString Returns self as ``year-mon-mday.''
upto
ref.upto( aDateMax ) {| date | block } -> ref Invokes block with dates from ref to aDateMax.
Library: English require "English" $OUTPUT_FIELD_SEPARATOR = ' -- ' "waterbuffalo" =~ /buff/ print $LOADED_FEATURES, $POSTMATCH, $PID, "\n" print $", $', $$, "\n" produces: English.rb -- alo -- 14806 -English.rb -- alo -- 14806 -Include the English library file in a Ruby script, and you can reference the global variables such as $_ using less cryptic names, listed in the following table. $* $? $< $> $! $@ $; $; $= $. $/
$ARGV $CHILD_STATUS $DEFAULT_INPUT $DEFAULT_OUTPUT $ERROR_INFO $ERROR_POSITION $FIELD_SEPARATOR $FS $IGNORECASE $INPUT_LINE_NUMBER $INPUT_RECORD_SEPARATOR
$" $& $. $, $\ $\ $, $$ $' $` $$
$LOADED_FEATURES $MATCH $NR $OFS $ORS $OUTPUT_RECORD_SEPARATOR $OUTPUT_FIELD_SEPARATOR $PID $POSTMATCH $PREMATCH $PROCESS_ID
http://www.rubycentral.com/book/lib_standard.html (9 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
$~ $LAST_MATCH_INFO $+ $LAST_PAREN_MATCH $_ $LAST_READ_LINE
$0 $PROGRAM_NAME $/ $RS
module Find Index: find prune
require "find" Find.find("/etc/passwd", "/var/spool/lp1", ".") do |f| Find.prune if f == "." puts f end produces: /etc/passwd /var/spool/lp1 /var/spool/lp1/status /var/spool/lp1/lock /var/spool/lp1/.seq The Find module supports the top-down traversal of a set of file paths.
class methods find
ref.find( [aName]* ) {| aFileName | block } Calls the associated block with the name of every file and directory listed as arguments, then recursively on their subdirectories, and so on.
prune
ref.prune Skips the current file or directory, restarting the loop with the next entry. If the current file is a directory, that directory will not be recursively entered. Meaningful only within the block associated with Find#find.
class File http://www.rubycentral.com/book/lib_standard.html (10 of 26) [7/20/2001 9:49:48 PM]
Parent: IO Version: 1.6
Programming Ruby: The Pragmatic Programmer's Guide
Index: cmp compare copy cp install makedirs mkpath move mv rm_f safe_unlink syscopy require 'ftools' File.copy 'testfile', 'testfile1' File.compare 'testfile', 'testfile1'
» true » true
The FTools library adds several methods to the built-in File class. These methods are particularly useful to programs that move and copy files, such as installers.
class methods cmp
ref.cmp( name1, name2, verbose=false ) -> true or false Synonym for File::compare.
compare
ref.compare( name1, name2, verbose=false ) -> true or false Returns true only if the contents of files name1 and name2 are identical.
copy
ref.copy( fromName, toName, verbose=false ) -> true or false Equivalent to calling File::syscopy, but logs the attempt to $stderr if verbose is not false.
cp
ref.cp( fromName, toName, verbose=false ) -> true or false Synonym for File::copy.
install
ref.install( fromName, toName, aMode=nil, verbose=false ) Copies file fromName to file toName using File::syscopy, unless toName already exists and has the same content as fromName. Sets the mode of the resulting file to aMode unless aMode is nil.
makedirs
ref.makedirs( [dirName]* [, aBoolean] ) Creates the given directories, logging each attempt to $stderr if the last parameter is true. Creates any missing parent directories as required.
mkpath
ref.mkpath( [dirName]* [, aBoolean] ) Synonym for File::makedirs.
move
ref.move( fromName, toName, verbose=false ) -> true or false
http://www.rubycentral.com/book/lib_standard.html (11 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Effectively renames fromName to toName, logging to $stderr if verbose is not false.
mv
ref.mv( fromName, toName, verbose=false ) -> true or false Synonym for File::move.
rm_f
ref.rm_f( [fileName]* [, aBoolean] ) -> anInteger Synonym for File::safe_unlink (the name refers to the Unix rm -f command).
safe_unlink
ref.safe_unlink( [fileName]* [, aBoolean] ) -> anInteger or nil Unlinks (deletes) the given files, logging to $stderr if the last parameter is true. The method attempts to make all files writable before unlinking them, so no errors will occur deleting read-only files. Returns the number of files deleted, or nil on error.
syscopy
ref.syscopy( fromName, toName ) -> true or false Efficiently copies the file named fromName to toName. If toName names a directory, the destination will be a file in that directory with the same basename as fromName. After the copy, the file mode of toName will be the same as that of fromName. Returns true on success.
class GetoptLong
Parent: Object Version: 1.6
Index: new each error? error_message get get_option ordering ordering= quiet quiet= quiet? set_options terminate terminated?
# Call using "ruby example.rb --size 10k -v -q a.txt b.doc" require 'getoptlong' # specify the options we accept and initialize # the option parser opts = GetoptLong.new( [ "--size", "-s", [ "--verbose", "-v", [ "--query", "-q", [ "--check", "--valid", "-c", )
GetoptLong::REQUIRED_ARGUMENT ], GetoptLong::NO_ARGUMENT ], GetoptLong::NO_ARGUMENT ], GetoptLong::NO_ARGUMENT ]
http://www.rubycentral.com/book/lib_standard.html (12 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
# process the parsed options opts.each do |opt, arg| puts "Option: #{opt}, arg #{arg.inspect}" end puts "Remaining args: #{ARGV.join(', ')}" produces: Option: --size, arg "10k" Option: --verbose, arg "" Option: --query, arg "" Remaining args: a.txt, b.doc Class GetoptLong supports GNU-style command-line option parsing. Options may be a minus sign (`-') followed by a single character, or two minus signs (`--') followed by a name (a long option). Long options may be abbreviated to their shortest unambiguous lengths. A single internal option may have multiple external representations. For example, the option to control verbose output could be any of -v, --verbose, or --details. Some options may also take an associated value. Each internal option is passed to GetoptLong as an array, containing strings representing the option's external forms and a flag. The flag (NO_ARGUMENT, REQUIRED_ARGUMENT, or OPTIONAL_ARGUMENT) specifies how GetoptLong is to associate an argument with the option. If the environment variable POSIXLY_CORRECT is set, all options must precede nonoptions on the command line. Otherwise, the default behavior of GetoptLong is to reorganize the command line to put the options at the front. This behavior may be changed by setting GetoptLong#ordering= to one of the constants PERMUTE, REQUIRE_ORDER, or RETURN_IN_ORDER. POSIXLY_CORRECT may not be overridden.
constants Per-option constants NO_ARGUMENT
Flags an option that takes no argument.
OPTIONAL_ARGUMENT
A nonoption following this option will be used as this option's argument.
REQUIRED_ARGUMENT
This option must be followed by an argument.
Overall constants PERMUTE
Options and their arguments will be shuffled to the front of the command line.
REQUIRE_ORDER
Options and their arguments must appear at the start of the command line. The first nonoption terminates option processing.
RETURN_IN_ORDER
Return options in the order in which they occur on the command line.
class methods new
GetoptLong.new( [options]* ) -> ref
http://www.rubycentral.com/book/lib_standard.html (13 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns a new option parser. Any options are passed to ref.set_options.
instance methods each
ref.each {| anOption, anArgument | block } Loops calling GetoptLong#get, passing the returned option and argument to the associated block. The loop ends when get returns nil for anOption.
error?
ref.error? -> anException Returns an Exception object documenting any error that has occurred, or nil if there has not been an error.
error_message
ref.error_message -> aString Returns the text of the last error message.
get
ref.get -> [ anOption, anArgument ] Returns the next option, along with any associated argument. If there is no argument, nil is returned for anArgument. If there are no remaining unprocessed options, or if there is an error in option processing and quiet has been set, nil is returned for anOption. Otherwise, if there is an error, a message is written to $stderr and an exception (a subclass of StandardError) is raised. The option string returned is the first option that was given in the corresponding array passed to set_options.
get_option
ref.get_option -> [ anOption, anArgument ] Synonym for GetoptLong#get.
ordering
ref.ordering -> aFixnum Returns the current ordering.
ordering=
ref.ordering = aFixnum Sets the ordering to one of PERMUTE, REQUIRE_ORDER, or RETURN_IN_ORDER. Quietly ignored if the environment variable POSIXLY_CORRECT is set. Ordering may not be changed once option processing has been started.
quiet
ref.quiet -> true or false Returns the current value of the quiet attribute.
http://www.rubycentral.com/book/lib_standard.html (14 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
quiet=
ref.quiet = true or false
Sets the current value of the quiet attribute. If false, any errors encountered are reported to $stderr.
quiet?
ref.quiet? -> true or false Synonym for GetoptLong#quiet.
set_options
ref.set_options( [anOptArray]* ) -> ref Each parameter is an array specifying a single internal option. The array contains one or more strings specifying the external form(s) of the option, and one of the flags NO_ARGUMENT, OPTIONAL_ARGUMENT, or REQUIRED_ARGUMENT. See the sample code on page 452 for examples of use.
terminate
ref.terminate -> ref Terminates option processing. Any remaining arguments are written back to ARGV. This may be called from within a GetoptLong#each or on its own. For example, calling the following program using ``ruby example.rb --size 10k -v -term -q a.txt b.doc'' will leave the -q and filenames in ARGV. require 'getoptlong' opts = GetoptLong.new( [ "--size", "-s", [ "--verbose", "-v", [ "--term", "-t", [ "--query", "-q", [ "--check", "--valid", "-c", )
GetoptLong::REQUIRED_ARGUMENT ], GetoptLong::NO_ARGUMENT ], GetoptLong::NO_ARGUMENT ], GetoptLong::NO_ARGUMENT ], GetoptLong::NO_ARGUMENT ]
opts.each do |opt, arg| puts "Option: #{opt}, arg #{arg.inspect}" opts.terminate if (opt == '--term') end puts "Remaining args: #{ARGV.join(', ')}" produces: Option: --size, arg "10k" Option: --verbose, arg "" Option: --term, arg "" Remaining args: -q, a.txt, b.doc
http://www.rubycentral.com/book/lib_standard.html (15 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
terminated?
ref.terminated? -> true or false Returns true if option processing has been terminated.
module mkmf Index: create_makefile dir_config find_library have_func have_header have_library The mkmf library is used by Ruby extension modules to help create Makefiles. When writing an extension, you create a program named ``extconf.rb'', which may be as simple as: require 'mkmf' create_makefile("Test") When run, this script will produce a Makefile suited to the target platform. mkmf contains several methods you can use to find libraries and include files and to set compiler flags. For more information on creating extension modules, see Chapter 17, which begins on page 171.
constants PLATFORM varies
A constant string that describes the platform on which Ruby is running, such as ``mswin32'' or ``i686-linux.''
$CFLAGS
Global variable for compiler flags.
$LDFLAGS
Global variable for linker flags.
instance methods create_makefile
create_makefile( target ) Creates a Makefile for an extension named target. If this method is not called, no Makefile is created.
dir_config
dir_config( name )
http://www.rubycentral.com/book/lib_standard.html (16 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Looks for directory configuration options for name given as arguments to this program or to the original build of Ruby. These arguments may be one of: --with-name-dir=directory --with-name-include=directory --with-name-lib=directory The given directories will be added to the appropriate search paths (include or link) in the Makefile.
find_library
find_library( name, function, [path]+ ) -> true or false Same as have_library, but will also search in the given directory paths.
have_func
have_func( function ) -> true or false If the named function exists in the standard compile environment, adds the directive -DHAVE_FUNCTION to the compile command in the Makefile and returns true.
have_header
have_header( header ) -> true or false If the given header file can be found in the standard search path, adds the directive -DHAVE_HEADER to the compile command in the Makefile and returns true.
have_library
have_library( library, function ) -> true or false If the given function exists in the named library, which must exist in the standard search path or in a directory added with dir_config, adds the library to the link command in the Makefile and returns true.
module ParseDate Index: parsedate The ParseDate module defines a single method, ParseDate#parsedate, which converts a date and/or time string into its constituents. It uses heuristics that handle a wide variety of date and time formats, including a subset of ISO 8601, Unix ctime, and most common written variants. The following table shows some examples. StringGuess? 1999-09-05 23:55:21+0900 1983-12-25 1965-11-10 T13:45
yy F 1999 F 1983 F 1965
mm 9 12 11
dd 5 25 10
hh 23 -13
min 55 -45
sec 21 ---
zone +0900 ---
wd ----
http://www.rubycentral.com/book/lib_standard.html (17 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
10/9/75 1:30pm 10/9/75 1:30pm Mon Feb 28 17:15:49 CST 2000 Tue, 02-Mar-99 11:20:32 GMT Tue, 02-Mar-99 11:20:32 GMT 12-January-1990, 04:00 WET 4/3/99 4/3/99 10th February, 1976 March 1st, 84 Friday
F T F F T F F T F T F
75 1975 2000 99 1999 1990 99 1999 1976 1984 --
10 10 2 3 3 1 4 4 2 3 --
9 9 28 2 2 12 3 3 10 1 --
13 13 17 11 11 4 ------
30 30 15 20 20 0 ------
--49 32 32 -------
--CST GMT GMT WET ------
--1 2 2 -----5
class methods parsedate
ref.parsedate( aString, guessYear=false ) -> [ year, mon, mday, hour, min, sec, zone, wday ] Parses a string containing a date and/or a time, returning an array of Fixnum objects containing the various components. nil is returned for fields that cannot be parsed from aString. If the result contains a year that is less than 100 and guessYear is true, parsedate will return a year value equal to year plus 2000 if year is less than 69, year plus 1900 otherwise.
Library: profile The profile library prints to $stderr a summary of the number of calls to, and the time spent in, each method in a Ruby program. The output is sorted by the total time spent in each method. Profiling can be enabled from the command line using the -rprofile option, or from within a source program by requiring the profile module. require 'profile' def ackerman(m, n) if m == 0 then n+1 elsif n == 0 and m > 0 then ackerman(m-1, 1) else ackerman(m-1, ackerman(m, n-1)) end end ackerman(3,3) produces: time 70.77 14.46 9.54 4.92 0.31 0.00 0.00
seconds 2.30 2.77 3.08 3.24 3.25 3.25 3.25
seconds 2.30 0.47 0.31 0.16 0.01 0.00 0.00
calls 2432 3676 2431 1188 57 1 1
ms/call 0.95 0.13 0.13 0.13 0.18 0.00 0.00
ms/call 41.42 0.13 0.13 0.13 0.18 0.00 3250.00
http://www.rubycentral.com/book/lib_standard.html (18 of 26) [7/20/2001 9:49:48 PM]
name Object#ackerman Fixnum#== Fixnum#Fixnum#+ Fixnum#> Module#method_added #toplevel
Programming Ruby: The Pragmatic Programmer's Guide
class PStore
Parent: Object Version: 1.6
Index: new [ ] [ ]= abort commit path root? roots transaction The PStore class provides transactional, file-based persistent storage of Ruby objects. The following example stores two hierarchies in a PStore. The first, identified by the key ``names'', is an array of Strings. The second, identified by ``tree'', is a simple binary tree. require "pstore" class T def initialize(val, left=nil, right=nil) @val, @left, @right = val, left, right end def to_a [ @val, @left.to_a, @right.to_a ] end end store = PStore.new("/tmp/store") store.transaction do store['names'] = [ 'Douglas', 'Barenberg', 'Meyer' ] store['tree'] = T.new('top', T.new('A', T.new('B')), T.new('C', T.new('D', nil, T.new('E')))) end # now read it back in store.transaction do puts "Roots: #{store.roots.join(', ')}" puts store['names'].join(', ') puts store['tree'].to_a.inspect end produces: Roots: names, tree Douglas, Barenberg, Meyer ["top", ["A", ["B", [], []], []], ["C", ["D", [], ["E", [], []]], []]] Each PStore can store several object hierarchies. Each hierarchy has a root, identified by a key (often a string). At the start of a PStore transaction, these hierarchies are read from a disk file and made available to the Ruby program. At the end of the transaction, the hierarchies are written back to the file. Any changes made to objects in these hierarchies are therefore saved on disk, to be read at the start of the next transaction that uses that file. In normal use, a PStore object is created and then is used one or more times to control a transaction. Within the body of the transaction, any object hierarchies that had previously been saved are made available, and any changes to object hierarchies, http://www.rubycentral.com/book/lib_standard.html (19 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
and any new hierarchies, are written back to the file at the end.
class methods new
PStore.new( aFilename ) -> aPStore Returns a new PStore object associated with the given file. If the file exists, its contents must have been previously written by PStore.
instance methods []
ref[ anObject ] -> anOtherObject Root Access---Returns the root of an object hierarchy identified by anObject. An exception is raised if anObject does not identify a root.
[ ]=
ref[ anObject ] = anOtherObject -> anOtherObject Root Creation---Sets anOtherObject as the base of the object hierarchy to be identified using anObject.
abort
ref.abort Terminates this transaction, losing any changes made to the object hierarchies.
commit
ref.commit Terminates the current transaction, saving the object hierarchies into the store's file.
path
ref.path -> aString Returns the name of the file associated with this store.
root?
ref.root?( anObject ) -> true or false Returns true if anObject is the key of a root in this store.
roots
ref.roots -> anArray Returns an array containing the keys of the root objects available in this store.
transaction
ref.transaction {| ref | block } -> anObject
http://www.rubycentral.com/book/lib_standard.html (20 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
If the file associated with ref exists, reads in the object hierarchies from it. It then executes the associated block, passing in ref. The block may use this parameter to access the roots of the hierarchies and hence access the persistent objects. If the block calls PStore#abort, or if it raises an exception, no data is saved back to the associated file. Otherwise, if it invokes PStore#commit, or if it terminates normally, the object hierarchies are written back to the file. The value returned is the value returned by the block.
class Tempfile
Parent: [IO] Version: 1.6
Index: new close open path require "tempfile" tf = Tempfile.new("afile") » tf.path tf.puts("Cosi Fan Tutte") » » tf.close » tf.open » tf.gets » tf.close(true)
"/tmp/afile14822.0" nil nil # "Cosi Fan Tutte\n" #\n0x40187c30
Class Tempfile creates managed temporary files. Although they behave the same as any other IO objects, temporary files are automatically deleted when the Ruby program terminates. Once a Tempfile object has been created, the underlying file may be opened and closed a number of times in succession. Tempfile does not directly inherit from IO. Instead, it delegates calls to a File object. From the programmer's perspective, apart from the unusual new, open, and close semantics, a Tempfile object behaves as if it were an IO object.
class methods new
Tempfile.new( basename, tmpdir=<see below> ) -> ref Constructs a temporary file in the given directory. The file name is built by concatenating basename, the current process id and (as an extension) a unique sequence number. If the tmpdir parameter is not supplied, it defaults to the value of one of the environment variables TMPDIR, TMP, or TEMP, or to the directory /tmp. The file is then opened using mode ``w+'', which allows reading and writing and deletes any existing content (see Table 22.5 on page 331).
open Tempfile.open( basename, tmpdir ) -> ref Synonym for Tempfile#new.
http://www.rubycentral.com/book/lib_standard.html (21 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
instance methods close
ref.close( final=false ) Closes ref. If final is true, deletes the underlying real file. If final is false, ref may be subsequently reopened. In all cases, the underlying file is deleted when the program terminates.
open
ref.open Reopens ref using mode ``r+'', which allows reading and writing but does not delete existing content.
path
ref.path -> aString Returns the full path of the underlying file.
class Mutex
Parent: Object Version: 1.6
Index: lock locked? synchronize try_lock unlock
require 'thread' sema4 = Mutex.new a = Thread.new { sema4.synchronize { # access shared resource } } b = Thread.new { sema4.synchronize { # access shared resource } } Mutex implements a simple semaphore that can be used to coordinate access to shared data from multiple concurrent threads.
http://www.rubycentral.com/book/lib_standard.html (22 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
instance methods lock
ref.lock -> ref Attempts to grab the lock and waits if it isn't available.
locked?
ref.locked? -> true or false Returns true if this lock is currently held by some thread.
synchronize
ref.synchronize { block } -> ref Obtains a lock (using Mutex#lock), runs the block, and releases the lock when the block completes.
try_lock
ref.try_lock -> true or false Attempts to obtain the lock and returns immediately. Returns true if the lock was granted.
unlock
ref.unlock -> ref or nil Releases the lock. Returns nil if ref wasn't locked.
class ConditionVariable Index: broadcast signal wait
http://www.rubycentral.com/book/lib_standard.html (23 of 26) [7/20/2001 9:49:48 PM]
Parent: Object Version: 1.6
Programming Ruby: The Pragmatic Programmer's Guide
require 'thread' mutex = Mutex.new resource = ConditionVariable.new a = Thread.new { mutex.synchronize { # Thread 'a' now needs the resource resource.wait(mutex) # 'a' can now have the resource } } b = Thread.new { mutex.synchronize { # Thread 'b' has finished using the resource resource.signal } } ConditionVariable objects augment class Mutex. Using condition variables, it is possible to suspend while in the middle of a critical section until a resource becomes available (see the discussion on page 119).
instance methods broadcast
ref.broadcast Wakes up all threads waiting for this lock.
signal
ref.signal Wakes up the first thread in line waiting for this lock.
wait
ref.wait( aMutex ) -> aMutex Releases the lock held in aMutex and waits; reacquires the lock on wakeup.
Library: timeout require "timeout" for snooze in 1..2 puts "About to sleep for #{snooze}" timeout(1.5) do sleep(snooze) end puts "That was refreshing" end
http://www.rubycentral.com/book/lib_standard.html (24 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
produces: About to sleep for 1 That was refreshing About to sleep for 2 /tc/usr/lib/ruby/1.6/timeout.rb:37: execution expired (TimeoutError) from prog.rb:5:in `timeout' from prog.rb:5 from prog.rb:3:in `each' from prog.rb:3 The timeout method takes a single parameter, representing a timeout period in seconds, and a block. The block is executed, and a timer is run concurrently. If the block terminates before the timeout, timeout returns true. Otherwise, a TimeoutError exception is raised.
class WeakRef
Parent: Delegator Version: 1.6
Index: new weakref_alive?
require "weakref" ref = "fol de rol" puts "Initial object is #{ref}" ref = WeakRef.new(ref) puts "Weak reference is #{ref}" ObjectSpace.garbage_collect puts "But then it is #{ref}" produces: Initial object is fol de rol Weak reference is fol de rol n finals=>1 0x40188a7c n finals=>0 prog.rb:8: Illegal Reference - probably recycled (WeakRef::RefError) In Ruby, objects are not eligible for garbage collection if there are still references to them. Normally, this is a Good Thing---it would be disconcerting to have an object simply evaporate while you were using it. However, sometimes you may need more flexibility. For example, you might want to implement an in-memory cache of commonly used file contents. As you read more files, the cache grows. At some point, you may run low on memory. The garbage collector will be invoked, but the objects in the cache are all referenced by the cache data structures, and so will not be deleted. A weak reference behaves exactly as any normal object reference with one important exception---the referenced object may be garbage collected, even while references to it exist. In the cache example, if the cached files were accessed using weak references, once memory runs low they will be garbage collected, freeing memory for the rest of the application. Weak references introduce a slight complexity. As the object referenced can be deleted by garbage collection at any time, code that accesses these objects must take care to ensure that the references are valid. Two techniques can be used. First, the code
http://www.rubycentral.com/book/lib_standard.html (25 of 26) [7/20/2001 9:49:48 PM]
Programming Ruby: The Pragmatic Programmer's Guide
can reference the objects normally. Any attempt to reference an object that has been garbage collected will raise a WeakRef::RefError exception. An alternative approach is to use the WeakRef#weakref_alive? method to check that a reference is valid before using it. Garbage collection must be disabled during the test and subsequent reference to the object. In a single-threaded program, you could use something like: ref = WeakRef.new(someObject) # # .. some time later # gcWasDisabled = GC.disable if ref.weakref_alive? # do stuff with 'ref' end GC.enable unless gcWasDisabled
class methods new
WeakRef.new( anObject ) -> ref Creates and returns a weak reference to anObject. All future references to anObject should be made using ref.
instance methods weakref_alive?
ref.weakref_alive? -> true or false Returns false if the object referenced by ref has been garbage collected.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Integer Version: 1.6
Index: Arithmetic operations Bit operations [ ] size to_f to_i to_s Bignum objects hold integers outside the range of Fixnum. Bignum objects are created automatically when integer calculations would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted. For the purposes of the bitwise operations and [], a Bignum is treated as if it were an infinite-length bitstring with 2's complement representation. While Fixnum values are immediate, Bignum objects are not---assignment and parameter passing work with references to objects, not the objects themselves.
instance methods Arithmetic operations Performs various arithmetic operations on big. big big big big big big
+ -* / % **
aNumeric aNumeric aNumeric aNumeric aNumeric aNumeric
Addition Subtraction Multiplication Division Modulo Exponentiation
Bit operations
http://www.rubycentral.com/book/ref_c_bignum.html (1 of 3) [7/20/2001 9:49:49 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Performs various operations on the binary representations of the Bignum. ~ big big | big & big ^ big >
aNumeric aNumeric aNumeric aNumeric aNumeric
Invert bits Bitwise OR Bitwise AND Bitwise EXCLUSIVE OR Left-shift aNumeric bits Right-shift aNumeric bits (with sign extension)
big aNumeric -> -1, 0, +1 Comparison---Returns -1, 0, or +1 depending on whether big is less than, equal to, or greater than aNumeric. This is the basis for the tests in Comparable.
[]
big[ n ] -> 0, 1 Bit Reference---Returns the nth bit in the (assumed) binary representation of big, where big[0] is the least significant bit. a = 9**15 50.downto(0) do |n| print a[n] end produces: 000101110110100000111000011110010100111100010111001
size
big.size -> anInteger Returns the number of bytes in the machine representation of big. (256**10 - 1).size (256**20 - 1).size (256**40 - 1).size
to_f
» » »
big.to_f -> aFloat Converts big to a Float. If big doesn't fit in a Float, the result is infinity.
to_i
big.to_i -> big Returns big.
http://www.rubycentral.com/book/ref_c_bignum.html (2 of 3) [7/20/2001 9:49:49 PM]
12 20 40
Programming Ruby: The Pragmatic Programmer's Guide
to_s
big.to_s -> aString Returns a string containing the decimal representation of big.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
class Binding
Parent: Object Version: 1.6
Index: Objects of class Binding encapsulate the execution context at some particular place in the code and retain this context for future use. The variables, methods, value of self, and possibly an iterator block that can be accessed in this context are all retained. Binding objects can be created using Kernel#binding, and are made available to the callback of Kernel#set_trace_func. These binding objects can be passed as the second argument of the Kernel#eval method, establishing an environment for the evaluation. class Demo def initialize(n) @secret = n end def getBinding return binding() end end k1 b1 k2 b2
= = = =
Demo.new(99) k1.getBinding Demo.new(-3) k2.getBinding
eval("@secret", b1) eval("@secret", b2) eval("@secret") Binding objects have no class-specific methods. http://www.rubycentral.com/book/ref_c_binding.html (1 of 2) [7/20/2001 9:49:50 PM]
» » »
99 -3 nil
Programming Ruby: The Pragmatic Programmer's Guide
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: call Continuation objects are generated by Kernel#callcc. They hold a return address and execution context, allowing a nonlocal return to the end of the callcc block from anywhere within a program. Continuations are somewhat analogous to a structured version of C's setjmp/longjmp (although they contain more state, so you might consider them closer to threads). For instance: arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ] callcc{|$cc|} puts(message = arr.shift) $cc.call unless message =~ /Max/ produces: Freddie Herbie Ron Max This (somewhat contrived) example allows the inner loop to abandon processing early:
http://www.rubycentral.com/book/ref_c_continuation.html (1 of 2) [7/20/2001 9:49:50 PM]
Programming Ruby: The Pragmatic Programmer's Guide
callcc {|cont| for i in 0..4 print "\n#{i}: " for j in i*5...(i+1)*5 cont.call() if j == 17 printf "%3d", j end end } print "\n" produces:
0: 1: 2: 3:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
instance methods call
cont.call( [args]* ) Invokes the continuation. The program continues from the end of the callcc block. If no arguments are given, the original callcc returns nil. If one argument is given, callcc returns it. Otherwise, an array containing args is returned. callcc {|cont| callcc {|cont| callcc {|cont|
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: [ ] chdir chroot delete entries foreach getwd glob mkdir new open pwd rmdir unlink close each read rewind seek tell Objects of class Dir are directory streams representing directories in the underlying file system. They provide a variety of ways to list directories and their contents. See also File, page 305. The directory used in these examples contains the two regular files (config.h and main.rb), the parent directory (..), and the directory itself (.).
mixins Enumerable:
collect, detect, each_with_index, entries, find, find_all, grep, include?, map, max, member?, min, reject, select, sort, to_a
class methods []
Dir[ aString ] -> anArray
http://www.rubycentral.com/book/ref_c_dir.html (1 of 6) [7/20/2001 9:49:51 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns anArray of filenames found by expanding the pattern given in aString. Note that this pattern is not a regexp (it's closer to a shell glob) and may contain the following metacharacters: Matches subdirectories recursively Matches zero or more characters Matches any single character Matches any character from the given set of characters. A range of characters is written as charFrom-charTo. The set may be negated with an initial uparrow (^). { opt, opt, ... } Matches any one of the optional strings ** * ? [ charSet ]
Dir["config.?"] Dir["*.[a-z][a-z]"] Dir["*.[^r]*"] Dir["*.{rb,h}"] Dir["*"]
chdir
» » » » »
["config.h"] ["main.rb"] ["config.h"] ["main.rb", "config.h"] ["config.h", "main.rb"]
Dir.chdir( [ aString] ) -> 0 Changes the current working directory of the process to the given string. When called without an argument, changes the directory to the value of the environment variable HOME, or LOGDIR. Raises a SystemCallError (probably Errno::ENOENT) if the target directory does not exist. Dir.chdir("/var/spool/mail") Dir.pwd
chroot
» 0 » "/var/spool/mail"
Dir.chroot( aString ) -> 0 Changes this process's idea of the file system root. Only a privileged process may make this call. Not available on all platforms. On Unix systems, see chroot(2) for more information. Dir.chdir("/production/secure/root") Dir.chroot("/production/secure/root") »0 Dir.pwd »"/"
delete
Dir.delete( aString ) -> 0 Deletes the named directory. Raises a subclass of SystemCallError if the directory isn't empty.
http://www.rubycentral.com/book/ref_c_dir.html (2 of 6) [7/20/2001 9:49:51 PM]
Programming Ruby: The Pragmatic Programmer's Guide
entries
Dir.entries( aString ) -> anArray Returns an array containing all of the filenames in the given directory. Will raise a SystemCallError if the named directory doesn't exist. Dir.entries("testdir") » [".", "..", "config.h", "main.rb"]
foreach
Dir.foreach( aString ) {| filename | block } -> nil Calls the block once for each entry in the named directory, passing the filename of each entry as a parameter to the block. Dir.foreach("testdir") {|x| puts("Got " + x) } produces: Got Got Got Got
getwd
. .. config.h main.rb
Dir.getwd -> aString Returns the path to the current working directory of this process as a string. Dir.chdir("/tmp") Dir.getwd
glob
» »
0 "/tmp"
Dir.glob( aString ) -> anArray Synonym for Dir::[].
mkdir
Dir.mkdir( aString [, anInteger] ) -> 0 Makes a new directory named by aString, with permissions specified by the optional parameter anInteger. The permissions may be modified by the value of File::umask, and are ignored on NT. Raises a SystemCallError if the directory cannot be created. See also the discussion of permissions on page 305.
http://www.rubycentral.com/book/ref_c_dir.html (3 of 6) [7/20/2001 9:49:51 PM]
Programming Ruby: The Pragmatic Programmer's Guide
new
Dir.new( aString ) -> aDir
Returns a new directory object for the named directory.
open
Dir.open( aString ) -> aDir Dir.open( aString ) {| aDir | block } -> nil With no block, open is a synonym for Dir::new. If a block is present, it is passed aDir as a parameter. The directory is closed at the end of the block, and Dir::open returns nil.
pwd
Dir.pwd -> aString Synonym for Dir::getwd.
rmdir
Dir.rmdir( aString ) -> true Synonym for Dir::delete.
unlink
Dir.unlink( aString ) -> true Synonym for Dir::delete.
instance methods close
dir.close -> nil Closes the directory stream. Any further attempts to access dir will raise an IOError. d = Dir.new("testdir") d.close
each
dir.each {| | block } -> dir
http://www.rubycentral.com/book/ref_c_dir.html (4 of 6) [7/20/2001 9:49:51 PM]
»
nil
Programming Ruby: The Pragmatic Programmer's Guide
Calls the block once for each entry in this directory, passing the filename of each entry as a parameter to the block. d = Dir.new("testdir") d.each {|x| puts ("Got " + x) } produces: Got Got Got Got
read
. .. config.h main.rb
dir.read -> aString or nil Reads the next entry from dir and returns it as a string. Returns nil at the end of the stream. d = Dir.new("testdir") » d.read » d.read » d.read
rewind
"." ".." "config.h"
dir.rewind -> dir Repositions dir to the first entry. d = Dir.new("testdir") » "." d.read » # d.rewind » "." d.read
seek
dir.seek( anInteger ) -> dir
http://www.rubycentral.com/book/ref_c_dir.html (5 of 6) [7/20/2001 9:49:51 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Seeks to a particular location in dir. anInteger must be a value returned by Dir#tell. d = Dir.new("testdir") » "." d.read i = d.tell » ".." d.read » # d.seek(i) » ".." d.read
tell
dir.tell -> anInteger Returns the current position in dir. See also Dir#seek. d = Dir.new("testdir") d.tell d.read d.tell
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: exception backtrace exception message set_backtrace Descendents of class Exception are used to communicate between raise methods and rescue statements in begin/end blocks. Exception objects carry information about the exception---its type (the exception's class name), an optional descriptive string, and optional traceback information.
class methods exception
Exception.exception( [aString] ) -> anException Creates and returns a new exception object, optionally setting the message to aString.
instance methods backtrace
exc.backtrace -> anArray
http://www.rubycentral.com/book/ref_c_exception.html (1 of 3) [7/20/2001 9:49:52 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns any backtrace associated with the exception. The backtrace is an array of strings, each containing either ``filename:lineNo: in `method''' or ``filename:lineNo.'' def a raise "boom" end def b a() end begin b() rescue => detail print detail.backtrace.join("\n") end produces: prog.rb:2:in `a' prog.rb:6:in `b' prog.rb:10
exception
exc.exception( [aString] ) -> anException or exc With no argument, returns the receiver. Otherwise, creates a new exception object of the same class as the receiver, but with a different message.
message
exc.message -> aString Returns the message associated with this exception.
set_backtrace
exc.set_backtrace( anArray ) -> anArray Sets the backtrace information associated with exc. The argument must be an array of String objects in the format described in Exception#backtrace.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide" Copyright © 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0. This reference is available for download.
http://www.rubycentral.com/book/ref_c_exception.html (3 of 3) [7/20/2001 9:49:52 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: &^| The global value false is the only instance of class FalseClass and represents a logically false value in boolean expressions. The class provides operators allowing false to participate correctly in logical expressions.
instance methods &
false & anObject -> false And---Returns false. anObject is always evaluated as it is the argument to a method call---there is no short-circuit evaluation in this case.
^
false ^ anObject -> true or false Exclusive Or---If anObject is nil or false, returns false; otherwise, returns true.
|
false | anObject -> true or false Or---Returns false if anObject is nil or false; true otherwise.
Previous
Programming Ruby: The Pragmatic Programmer's Guide Copyright © 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0. This reference is available for download.
http://www.rubycentral.com/book/ref_c_falseclass.html (2 of 2) [7/20/2001 9:49:52 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: atime blksize blockdev? blocks chardev? ctime dev directory? executable? executable_real? file? ftype gid grpowned? ino mode mtime nlink owned? pipe? rdev readable? readable_real? setgid? setuid? size size? socket? sticky? symlink? uid writable? writable_real? zero? Objects of class File::Stat encapsulate common status information for File objects. The information is recorded at the moment the File::Stat object is created; changes made to the file after that point will not be reflected. File::Stat objects are returned by IO#stat, File::stat, File#lstat, and File::lstat. Many of these methods return platform-specific values, and not all values are meaningful on all systems. See also Kernel#test on page 429.
mixins Comparable:
, between?
instance methods
statfile anOtherStat -> -1, 0, 1 Compares File::Stat objects by comparing their respective modification times. f1 = File.new("f1", "w") sleep 1 f2 = File.new("f2", "w") f1.stat f2.stat
atime
»
-1
statfile.atime -> aTime Returns the last access time for this file as an object of class Time. File.stat("testfile").atime » Wed Dec 31 18:00:00 CST 1969
http://www.rubycentral.com/book/ref_c_file__stat.html (1 of 6) [7/20/2001 9:49:53 PM]
Programming Ruby: The Pragmatic Programmer's Guide
blksize
statfile.blksize -> anInteger Returns the native file system's block size. Will return 0 on platforms that don't support this information. File.stat("testfile").blksize
blockdev?
» 4096
statfile.blockdev? -> true or false Returns true if the file is a block device, false if it isn't or if the operating system doesn't support this feature. File.stat("testfile").blockdev?
blocks
» false
statfile.blocks -> anInteger Returns the number of native file system blocks allocated for this file, or 0 if the operating system doesn't support this feature. File.stat("testfile").blocks
chardev?
» 2
statfile.chardev? -> true or false Returns true if the file is a character device, false if it isn't or if the operating system doesn't support this feature. File.stat("/dev/tty").chardev?
ctime
» true
statfile.ctime -> aTime Returns the change time for statfile (that is, the time directory information about the file was changed, not the file itself). File.stat("testfile").ctime » Sun Mar 04 23:28:25 CST 2001
dev
statfile.dev -> aFixnum Returns an integer representing the device on which statfile resides. File.stat("testfile").dev
directory?
» 774
statfile.directory? -> true or false Returns true if statfile is a directory, false otherwise. File.stat("testfile").directory? File.stat(".").directory?
http://www.rubycentral.com/book/ref_c_file__stat.html (2 of 6) [7/20/2001 9:49:53 PM]
» false » true
Programming Ruby: The Pragmatic Programmer's Guide
executable?
statfile.executable? -> true or false Returns true if statfile is executable or if the operating system doesn't distinguish executable files from nonexecutable files. The tests are made using the effective owner of the process. » false
File.stat("testfile").executable?
executable_real?
statfile.executable_real? -> true or false Same as executable?, but tests using the real owner of the process.
file?
statfile.file? -> true or false Returns true if statfile is a regular file (not a device file, pipe, socket, etc.). » true
File.stat("testfile").file?
ftype
statfile.ftype -> fileType Identifies the type of statfile. The return string is one of: ``file'', ``directory'', ``characterSpecial'', ``blockSpecial'', ``fifo'', ``link'', or ``socket''. File.stat("/dev/tty").ftype
gid
» "characterSpecial"
statfile.gid -> aFixnum Returns the numeric group id of the owner of statfile. File.stat("testfile").gid
grpowned?
» 500
statfile.grpowned? -> true or false Returns true if the effective group id of the process is the same as the group id of statfile. On Windows NT, returns false. File.stat("testfile").grpowned? File.stat("/etc/passwd").grpowned?
ino
» true » false
statfile.ino -> aFixnum Returns the inode number for statfile. File.stat("testfile").ino
mode
statfile.mode -> aFixnum
http://www.rubycentral.com/book/ref_c_file__stat.html (3 of 6) [7/20/2001 9:49:53 PM]
» 43331
Programming Ruby: The Pragmatic Programmer's Guide
Returns an integer representing the permission bits of statfile. The meaning of the bits is platform dependent; on Unix systems, see stat(2). File.chmod(0644, "testfile") s = File.stat("testfile") sprintf("%o", s.mode)
mtime
» 1 » "100644"
statfile.mtime -> aTime Returns the modification time for statfile. File.stat("testfile").mtime » Sun Mar 04 23:28:25 CST 2001
nlink
statfile.nlink -> aFixnum Returns the number of hard links to statfile. » 1 » 0 » 2
File.stat("testfile").nlink File.link("testfile", "testfile.bak") File.stat("testfile").nlink
owned?
statfile.owned? -> true or false Returns true if the effective user id of the process is the same as the owner of statfile. File.stat("testfile").owned? File.stat("/etc/passwd").owned?
pipe?
» true » false
statfile.pipe? -> true or false Returns true if the operating system supports pipes and statfile is a pipe; false otherwise.
rdev
statfile.rdev -> aFixnum Returns an integer representing the device type on which statfile resides. Returns 0 if the operating system doesn't support this feature. File.stat("/dev/fd0").rdev
readable?
» 512
statfile.readable? -> true or false Returns true if statfile is readable by the effective user id of this process. File.stat("testfile").readable?
http://www.rubycentral.com/book/ref_c_file__stat.html (4 of 6) [7/20/2001 9:49:53 PM]
» true
Programming Ruby: The Pragmatic Programmer's Guide
readable_real?
statfile.readable_real? -> true or false
Returns true if statfile is readable by the real user id of this process. » true
File.stat("testfile").readable_real?
setgid?
statfile.setgid? -> true or false Returns true if statfile has the set-group-id permission bit set, false if it doesn't or if the operating system doesn't support this feature. » true
File.stat("/usr/sbin/lpc").setgid?
setuid?
statfile.setuid? -> true or false Returns true if statfile has the set-user-id permission bit set, false if it doesn't or if the operating system doesn't support this feature. » true
File.stat("/bin/su").setuid?
size
statfile.size -> aFixnum Returns the size of statfile in bytes. » 66
File.stat("testfile").size
size?
statfile.size? -> aFixnum or nil Returns nil if statfile is a zero-length file; otherwise, returns the file size. File.stat("testfile").size?
socket?
» 66
statfile.socket? -> true or false Returns true if statfile is a socket, false if it isn't or if the operating system doesn't support this feature. File.stat("testfile").socket?
sticky?
» false
statfile.sticky? -> true or false Returns true if statfile has its sticky bit set, false if it doesn't or if the operating system doesn't support this feature. File.stat("testfile").sticky?
http://www.rubycentral.com/book/ref_c_file__stat.html (5 of 6) [7/20/2001 9:49:53 PM]
» false
Programming Ruby: The Pragmatic Programmer's Guide
symlink?
statfile.symlink? -> true or false Returns true if statfile is a symbolic link, false if it isn't or if the operating system doesn't support this feature. As File::stat automatically follows symbolic links, symlink? will always be false for an object returned by File::stat. » 0 » false » true
File.symlink("testfile", "alink") File.stat("alink").symlink? File.lstat("alink").symlink?
uid
statfile.uid -> aFixnum Returns the numeric user id of the owner of statfile. » 501
File.stat("testfile").uid
writable?
statfile.writable? -> true or false Returns true if statfile is writable by the effective user id of this process. File.stat("testfile").writable?
writable_real?
» true
statfile.writable_real? -> true or false Returns true if statfile is writable by the real user id of this process. File.stat("testfile").writable_real?
zero?
» true
statfile.zero? -> true or false Returns true if statfile is a zero-length file; false otherwise. File.stat("testfile").zero?
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Integer Version: 1.6
Index: Arithmetic operations Bit operations [ ] id2name size to_f to_i to_s A Fixnum holds Integer values that can be represented in a native machine word (minus 1 bit). If any operation on a Fixnum exceeds this range, the value is automatically converted to a Bignum. Fixnum objects have immediate value. This means that when they are assigned or passed as parameters, the actual object is passed, rather than a reference to that object. Assignment does not alias Fixnum objects. There is effectively only one Fixnum object instance for any given integer value, so, for example, you cannot add a singleton method to a Fixnum.
instance methods Arithmetic operations Performs various arithmetic operations on fix. fix fix fix fix fix fix
+ -* / % **
aNumeric aNumeric aNumeric aNumeric aNumeric aNumeric
Addition Subtraction Multiplication Division Modulo Exponentiation
Bit operations
http://www.rubycentral.com/book/ref_c_fixnum.html (1 of 3) [7/20/2001 9:49:54 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Performs various operations on the binary representations of the Fixnum. ~ fix fix | fix & fix ^ fix >
aNumeric aNumeric aNumeric aNumeric aNumeric
Invert bits Bitwise OR Bitwise AND Bitwise EXCLUSIVE OR Left-shift aNumeric bits Right-shift aNumeric bits (with sign extension)
fix aNumeric -> -1, 0, +1 Comparison---Returns -1, 0, or +1 depending on whether fix is less than, equal to, or greater than aNumeric. This is the basis for the tests in Comparable.
[]
fix[ n ] -> 0, 1 Bit Reference---Returns the nth bit in the binary representation of fix, where fix[0] is the least significant bit. a = 0b11001100101010 30.downto(0) do |n| print a[n] end produces: 0000000000000000011001100101010
id2name
fix.id2name -> aString or nil Returns the name of the object whose symbol id is the value of fix. If there is no symbol in the symbol table with this value, returns nil. id2name has nothing to do with the Object.id method. See also String#intern on page 376 and class Symbol on page 388. symbol = :@inst_var id = symbol.to_i id.id2name
size
» :@inst_var » 8978 » "@inst_var"
fix.size -> aFixnum Returns the number of bytes in the machine representation of a Fixnum.
http://www.rubycentral.com/book/ref_c_fixnum.html (2 of 3) [7/20/2001 9:49:54 PM]
Programming Ruby: The Pragmatic Programmer's Guide
to_f
fix.to_f -> aFloat
Converts fix to a Float.
to_i
fix.to_i -> fix Returns fix.
to_s
fix.to_s -> aString Returns a string containing the decimal representation of self.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Numeric Version: 1.6
Index: Arithmetic operations ceil finite? floor infinite? nan? round to_f to_i to_s Float objects represent real numbers using the native architecture's double-precision floating point representation.
instance methods Arithmetic operations Performs various arithmetic operations on flt. flt flt flt flt flt flt
+ -* / % **
aNumeric aNumeric aNumeric aNumeric aNumeric aNumeric
Addition Subtraction Multiplication Division Modulo Exponentiation
flt aNumeric -> -1, 0, +1 Returns -1, 0, or +1 depending on whether flt is less than, equal to, or greater than aNumeric. This is the basis for the tests in Comparable.
ceil
flt.ceil -> anInteger
http://www.rubycentral.com/book/ref_c_float.html (1 of 3) [7/20/2001 9:49:54 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns the smallest Integer greater than or equal to flt. » » » »
1.2.ceil 2.0.ceil (-1.2).ceil (-2.0).ceil
finite?
2 2 -1 -2
flt.finite? -> true or false Returns true if flt is a valid IEEE floating point number (it is not infinite, and nan? is false).
floor
flt.floor -> anInteger Returns the largest integer less than or equal to flt. » » » »
1.2.floor 2.0.floor (-1.2).floor (-2.0).floor
infinite?
1 2 -2 -2
flt.infinite? -> nil, -1, +1 Returns nil, -1, or +1 depending on whether flt is finite, -infinity, or +infinity. » » »
(0.0).infinite? (-1.0/0.0).infinite? (+1.0/0.0).infinite?
nan?
flt.nan? -> true or false Returns true if flt is an invalid IEEE floating point number. a = -1.0 a.nan? a = Math.log(a) a.nan?
round
flt.round -> anInteger
http://www.rubycentral.com/book/ref_c_float.html (2 of 3) [7/20/2001 9:49:54 PM]
» » » »
-1.0 false NaN true
nil -1 1
Programming Ruby: The Pragmatic Programmer's Guide
Rounds flt to the nearest integer. Equivalent to: def round return floor(self+0.5) if self > 0.0 return ceil(self-0.5) if self < 0.0 return 0.0 end 1.5.round (-1.5).round
to_f
» »
2 -2
flt.to_f -> flt Returns flt.
to_i
flt.to_i -> anInteger Returns flt truncated to an Integer.
to_s
flt.to_s -> aString Returns a string containing a representation of self. As well as a fixed or exponential form of the number, the call may return ``NaN'', ``Infinity'', and ``-Infinity''.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
class Integer
Parent: Numeric Version: 1.6
Index: chr downto integer? next step succ times upto Subclasses: Bignum, Fixnum
Integer is the basis for the two concrete classes that hold whole numbers, Bignum and Fixnum.
instance methods chr
int.chr -> aString Returns a string containing the ASCII character represented by the receiver's value. 65.chr ?a.chr 230.chr
downto
» » »
int.downto( anInteger ) {| i | block } -> int
http://www.rubycentral.com/book/ref_c_integer.html (1 of 3) [7/20/2001 9:49:55 PM]
"A" "a" "\346"
Programming Ruby: The Pragmatic Programmer's Guide
Iterates block, passing decreasing values from int down to and including anInteger. 5.downto(1) { |n| print n, ".. " } print " Liftoff!\n" produces: 5.. 4.. 3.. 2.. 1..
integer?
Liftoff!
int.integer? -> true Always returns true.
next
int.next -> anInteger Returns the Integer equal to int + 1. 1.next (-1).next
step
» »
2 0
int.step( endNum, step ) {| i | block } -> int Invokes block with the sequence of numbers starting at int, incremented by step on each call. The loop finishes when the value to be passed to the block is greater than endNum (if step is positive) or less than endNum (if step is negative). 1.step(10, 2) { |i| print i, " " } produces: 1 3 5 7 9
succ
int.succ -> anInteger Synonym for Integer#next.
times
int.times {| i | block } -> int
http://www.rubycentral.com/book/ref_c_integer.html (2 of 3) [7/20/2001 9:49:55 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Iterates block int times, passing in values from zero to int - 1. 5.times do |i| print i, " " end print "\n" produces: 0 1 2 3 4
upto
int.upto( anInteger ) {| i | block } -> int Iterates block, passing in integer values from int up to and including anInteger. 5.upto(10) { |i| print i, " " } produces: 5 6 7 8 9 10
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: [ ] begin end length offset post_match pre_match size string to_a to_s MatchData is the type of the special variable $~, and is the type of the object returned by Regexp#match and Regexp#last_match. It encapsulates all the results of a pattern match, results normally accessed through the special variables $&, $', $`, $1, $2, and so on.
instance methods []
mtch[i] -> anObject mtch[start, length] -> anArray mtch[aRange] -> anArray Match Reference---MatchData acts as an array, and may be accessed using the normal array indexing techniques. mtch[0] is equivalent to the special variable $&, and returns the entire matched string. mtch[1], mtch[2], and so on return the values of the matched backreferences (portions of the pattern between parentheses). m = /(.)(.)(\d+)(\d)/.match("THX1138.") » "HX1138" m[0] » ["H", "X"] m[1, 2] » ["H", "X", "113"] m[1..3] » ["X", "113"] m[-3, 2]
begin
mtch.begin( n ) -> anInteger
http://www.rubycentral.com/book/ref_c_matchdata.html (1 of 3) [7/20/2001 9:49:55 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns the offset of the start of the nth element of the match array in the string. m = /(.)(.)(\d+)(\d)/.match("THX1138.") m.begin(0) m.begin(2)
end
» »
1 2
mtch.end( n ) -> anInteger Returns the offset of the character immediately following the end of the nth element of the match array in the string. m = /(.)(.)(\d+)(\d)/.match("THX1138.") m.end(0) m.end(2)
length
» »
7 3
» »
5 5
mtch.length -> anInteger Returns the number of elements in the match array. m = /(.)(.)(\d+)(\d)/.match("THX1138.") m.length m.size
offset
mtch.offset( n ) -> anArray Returns a two-element array containing the beginning and ending offsets of the nth match. m = /(.)(.)(\d+)(\d)/.match("THX1138.") » m.offset(0) [1, 7] » m.offset(4) [6, 7]
post_match
mtch.post_match -> aString Returns the portion of the original string after the current match. Equivalent to the special variable $'. m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie") » ": The Movie" m.post_match
pre_match
mtch.pre_match -> aString
http://www.rubycentral.com/book/ref_c_matchdata.html (2 of 3) [7/20/2001 9:49:55 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns the portion of the original string before the current match. Equivalent to the special variable $`. m = /(.)(.)(\d+)(\d)/.match("THX1138.") m.pre_match
size
»
"T"
mtch.size -> anInteger A synonym for MatchData#length.
string
mtch.string -> aString Returns a frozen copy of the string passed in to match. m = /(.)(.)(\d+)(\d)/.match("THX1138.") » "THX1138." m.string
to_a
mtch.to_a -> anArray Returns the array of matches. m = /(.)(.)(\d+)(\d)/.match("THX1138.") » ["HX1138", "H", "X", "113", "8"] m.to_a
to_s
mtch.to_s -> aString Returns the entire matched string. m = /(.)(.)(\d+)(\d)/.match("THX1138.") » m.to_s "HX1138"
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: & ^ | nil? to_a to_i to_s The class of the singleton object nil.
instance methods &
nil& anObject -> false And---Returns false. As anObject is an argument to a method call, it is always evaluated; there is no short-circuit evaluation in this case. nil && puts("logical and") nil & puts("and") produces: and
^
nil^ anObject -> true or false Exclusive Or---Returns false if anObject is nil or false, true otherwise.
|
nil| anObject -> true or false Or---Returns false if anObject is nil or false, true otherwise.
http://www.rubycentral.com/book/ref_c_nilclass.html (1 of 2) [7/20/2001 9:49:56 PM]
Programming Ruby: The Pragmatic Programmer's Guide
nil?
nil.nil? -> true Always returns true.
to_a
nil.to_a -> [] Always returns an empty array.
to_i
nil.to_i -> 0 Always returns zero.
to_s
nil.to_s -> "" Always returns the empty string.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: +@ --@ abs coerce divmod eql? integer? modulo nonzero? remainder zero?
Subclasses: Float, Integer
Numeric is the fundamental base type for the concrete number classes Float, Fixnum, and Bignum.
mixins Comparable:
, between?
Difference between modulo and remainder. The modulo operator (``%'') always has the sign of the divisor, whereas remainder has the sign of the dividend. a 13 13 -13 -13 11.5 11.5 -11.5 -11.5
b 4 -4 4 -4 4 -4 4 -4
a.divmod(b) 3, 1 -4, -3 -4, 3 3, -1 2.0, 3.5 -3.0, -0.5 -3.0, 0.5 2.0, -3.5
a/b 3 -4 -4 3 2.875 -2.875 -2.875 2.875
a.modulo(b) 1 -3 3 -1 3.5 -0.5 0.5 -3.5
a.remainder(b) 1 1 -1 -1 3.5 3.5 -3.5 -3.5
instance methods +@
+num -> num Unary Plus---Returns the receiver's value.
--@
--num -> aNumeric
http://www.rubycentral.com/book/ref_c_numeric.html (1 of 3) [7/20/2001 9:49:56 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Unary Minus---Returns the receiver's value, negated.
abs
num.abs -> aNumeric Returns the absolute value of num. » » »
12.abs (-34.56).abs -34.56.abs
coerce
12 34.56 34.56
num.coerce( aNumeric ) -> anArray If aNumeric is the same type as num, returns an array containing aNumeric and num. Otherwise, returns an array with both aNumeric and num represented as Float objects. 1.coerce(2.5) 1.2.coerce(3) 1.coerce(2)
divmod
» » »
[2.5, 1.0] [3.0, 1.2] [2, 1]
num.divmod( aNumeric ) -> anArray Returns an array containing the quotient and modulus obtained by dividing num by aNumeric. If q, r = x.divmod(y), q = floor(float(x) / float(y)) x = q*y+r The quotient is rounded toward -infinity. See Table 22.6 on page 355. 11.divmod(3) 11.divmod(-3) 11.divmod(3.5) (-11).divmod(3.5) (11.5).divmod(3.5)
eql?
» » » » »
[3, 2] [-4, -1] [3.0, 0.5] [-4.0, 3.0] [3.0, 1.0]
num.eql?( aNumeric ) -> true or false Returns true if num and aNumeric are the same type and have equal values. 1 == 1.0 1.eql?(1.0) (1.0).eql?(1.0)
integer?
» » »
true false true
num.integer? -> true or false Returns true if num is an Integer (including Fixnum and Bignum).
modulo
num.modulo( aNumeric ) -> aNumeric Equivalent to num.divmod(aNumeric)[1].
http://www.rubycentral.com/book/ref_c_numeric.html (2 of 3) [7/20/2001 9:49:56 PM]
Programming Ruby: The Pragmatic Programmer's Guide
nonzero?
num.nonzero? -> num or nil Returns num if num is not zero, nil otherwise. This behavior is useful when chaining comparisons: a = %w( z Bb bB bb BB a aA Aa AA A ) b = a.sort {|a,b| (a.downcase b.downcase).nonzero? || a b } b » ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
remainder
num.remainder( aNumeric ) -> aNumeric If num and aNumeric have different signs, returns mod-aNumeric; otherwise, returns mod. In both cases mod is the value num.modulo(aNumeric). The differences between remainder and modulo (%) are shown in Table 22.6 on page 355.
zero?
num.zero? -> true or false Returns true if num has a zero value.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: new === begin each end exclude_end? first last length size A Range represents an interval---a set of values with a start and an end. Ranges may be constructed using the s..e and s...e literals, or with Range::new. Ranges constructed using .. run from the start to the end inclusively. Those created using ... exclude the end value. When used as an iterator, ranges return each value in the sequence. (-1..-5).to_a (-5..-1).to_a ('a'..'e').to_a ('a'...'e').to_a
» » » »
[] [-5, -4, -3, -2, -1] ["a", "b", "c", "d", "e"] ["a", "b", "c", "d"]
Ranges can be constructed using objects of any type, as long as the objects can be compared using their operator and they support the succ method to return the next object in sequence. class Xs # represent a string of 'x's include Comparable attr :length def initialize(n) @length = n end def succ Xs.new(@length + 1) end def (other) raise TypeError unless other.kind_of? Xs @length other.length end def inspect 'x' * @length http://www.rubycentral.com/book/ref_c_range.html (1 of 4) [7/20/2001 9:49:57 PM]
Programming Ruby: The Pragmatic Programmer's Guide
end end r = Xs.new(3)..Xs.new(6) r.to_a r.member?(Xs.new(5))
» xxx..xxxxxx » [xxx, xxxx, xxxxx, xxxxxx] » true
mixins Enumerable:
collect, detect, each_with_index, entries, find, find_all, grep, include?, map, max, member?, min, reject, select, sort, to_a
class methods new
Range.new( start, end, exclusive=false ) -> aRange Constructs a range using the given start and end. If the third parameter is omitted or is false, the range will include the end object; otherwise, it will be excluded.
instance methods ===
rng === anObject -> true or false Returns true if anObject is an element of rng, false otherwise. Conveniently, === is the comparison operator used by case statements. case when when when end
79 1..50 then 51..75 then 76..100 then
print "low\n" print "medium\n" print "high\n"
produces: high
begin
rng.begin -> anObject Returns the first object of rng.
each
rng.each {| i | block } -> rng
http://www.rubycentral.com/book/ref_c_range.html (2 of 4) [7/20/2001 9:49:57 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Iterates over the elements rng, passing each in turn to the block. (10..15).each do |n| print n, ' ' end produces: 10 11 12 13 14 15
end
rng.end -> anObject Returns the object that defines the end of rng. See also Range#length. » »
(1..10).end (1...10).end
exclude_end?
10 10
rng.exclude_end? -> true or false Returns true if rng excludes its end value.
first
rng.first -> anObject Returns the first object in rng.
last
rng.last -> anObject Synonym for Range#end.
length
rng.length -> anInteger Returns the number of objects in rng. » »
(1..10).length (1...10).length
size
10 9
rng.size -> anInteger Synonym for Range#length.
Previous
Programming Ruby: The Pragmatic Programmer's Guide This reference is available for download.
http://www.rubycentral.com/book/ref_c_range.html (4 of 4) [7/20/2001 9:49:57 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: new new members == [ ] [ ]= each length members size to_a values Subclasses: Struct::Tms
A Struct is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class. The Struct class is a generator of specific classes, each one of which is defined to hold a set of variables and their accessors. In these examples, we'll call the generated class ``CustomerClass,'' and we'll show an example instance of that class as ``CustomerInst.'' In the descriptions that follow, the parameter aSymbol refers to a symbol, which is either a quoted string or a Symbol (such as :name).
mixins Enumerable:
collect, detect, each_with_index, entries, find, find_all, grep, include?, map, max, member?, min, reject, select, sort, to_a
class methods new
Struct.new( [aString] [, aSym]+ ) -> CustomerClass Creates a new class, named by aString, containing accessor methods for the given symbols. If the name aString is omitted, an anonymous structure class will be created. Otherwise, the name of this struct will appear as a constant in class Struct, so it must be unique for all Structs in the system and should start with a capital letter. Struct::new returns a new Class object, which can then be used to create specific instances of the new structure. The remaining methods listed below (class and instance) are defined for this generated class. See the description that follows for an example.
new
CustomerClass.new( [anObject]+ ) -> CustomerInst
http://www.rubycentral.com/book/ref_c_struct.html (1 of 4) [7/20/2001 9:49:58 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Creates a new instance. The number of actual parameters must be less than or equal to the number of attributes defined for this class; unset parameters default to nil. Passing too many parameters will raise an ArgumentError. Customer = Struct.new( "Customer", :name, :address, :zip ) joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 ) » joe.name "Joe Smith" » joe.zip 12345
members
CustomerClass.members -> anArray Returns an array of strings representing the names of the instance variables. Customer = Struct.new( "Customer", :name, :address, :zip ) » ["name", "address", "zip"] Customer.members
instance methods ==
CustomerInst == anOtherStruct -> true or false Equality---Returns true if anOtherStruct is equal to this one: they must be of the same class as generated by Struct::new, and the values of all instance variables must be equal (according to Object#==). Customer = Struct.new( "Customer", :name, :address, :zip ) joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 ) joejr = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 ) jane = Customer.new( "Jane Doe", "456 Elm, Anytown NC", 12345 ) » joe == joejr true » joe == jane false
[]
CustomerInst[ aSymbol ] -> anObject CustomerInst[ anInteger ] -> anObject Attribute Reference---Returns the value of the instance variable named by aSymbol, or indexed (0..length-1) by anInteger. Will raise NameError if the named variable does not exist, or IndexError if the index is out of range. Customer = Struct.new( "Customer", :name, :address, :zip ) joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 ) joe["name"] joe[:name] joe[0]
[ ]=
» » »
CustomerInst[ aSymbol ] = anObject -> anObject CustomerInst[ anInteger ] = anObject -> anObject
http://www.rubycentral.com/book/ref_c_struct.html (2 of 4) [7/20/2001 9:49:58 PM]
"Joe Smith" "Joe Smith" "Joe Smith"
Programming Ruby: The Pragmatic Programmer's Guide
Attribute Assignment---Assigns to the instance variable named by aSymbol or anInteger the value anObject and returns it. Will raise a NameError if the named variable does not exist, or an IndexError if the index is out of range. Customer = Struct.new( "Customer", :name, :address, :zip ) joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 ) joe["name"] = "Luke" joe[:zip] = "90210" joe.name joe.zip
each
» »
"Luke" "90210"
CustomerInst.each {| anObject | block } -> CustomerInst Calls block once for each instance variable, passing the value as a parameter. Customer = Struct.new( "Customer", :name, :address, :zip ) joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 ) joe.each {|x| puts(x) } produces: Joe Smith 123 Maple, Anytown NC 12345
length
CustomerInst.length -> anInteger Returns the number of instance variables. Customer = Struct.new( "Customer", :name, :address, :zip ) joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 ) » joe.length 3
members
CustomerInst.members -> anArray Returns an array of strings representing the names of the instance variables. Customer = Struct.new( "Customer", :name, :address, :zip ) joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 ) » ["name", "address", "zip"] joe.members
size
CustomerInst.size -> anInteger Synonym for Struct#length.
http://www.rubycentral.com/book/ref_c_struct.html (3 of 4) [7/20/2001 9:49:58 PM]
Programming Ruby: The Pragmatic Programmer's Guide
to_a
CustomerInst.to_a -> anArray
Returns the values for this instance as an array. Customer = Struct.new( "Customer", :name, :address, :zip ) joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 ) » "123 Maple, Anytown NC" joe.to_a[1]
values
CustomerInst.values -> anArray Synonym for to_a.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Struct Version: 1.6
Index: This structure is returned by Time::times. It holds information on process times on those platforms that support it. Not all values are valid on all platforms. This structure contains the following instance variables and the corresponding accessors: utime stime cutime cstime
Amount of User CPU time, in seconds Amount of System CPU time, in seconds Completed child processes' User CPU time, in seconds (always 0 on Windows NT) Completed child processes' System CPU time, in seconds (always 0 on Windows NT)
See also Struct on page 385 and Time::times on page 398. t = Time.times [ t.utime, t.stime] [ t.cutime, t.cstime ]
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: id2name inspect to_i to_s A Symbol object represents a Ruby name and is generated automatically using the :name literal syntax. The same Symbol object will be created for a given name string for the duration of a program's execution, regardless of the context or meaning of that name. Thus if Fred is a constant in one context, a method in another, and a class in a third, the Symbol :Fred will be the same object in all three contexts. module One class Fred end $f1 = :Fred end module Two Fred = 1 $f2 = :Fred end def Fred() end $f3 = :Fred $f1.id $f2.id $f3.id
» » »
2299150 2299150 2299150
http://www.rubycentral.com/book/ref_c_symbol.html (1 of 2) [7/20/2001 9:49:58 PM]
Programming Ruby: The Pragmatic Programmer's Guide
instance methods id2name
sym.id2name -> aString Returns the name corresponding to sym. »
:fred.id2name
inspect
"fred"
sym.inspect -> aString Returns the representation of sym as a symbol literal. »
:fred.inspect
to_i
":fred"
sym.to_i -> aFixnum Returns an integer that is unique for each symbol within a particular execution of a program.
to_s
sym.to_s -> aString Synonym for Symbol#id2name.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: new add list ThreadGroup provides a means of keeping track of a number of threads as a group. A Thread can belong to only one ThreadGroup at a time; adding a thread to a new group will remove it from any previous group. Newly created threads belong to the same group as the thread from which they were created.
constants Default
Default thread group.
class methods new
ThreadGroup.new -> thgrp Returns a newly created ThreadGroup. The group is initially empty.
instance methods add
thgrp.add( aThread ) -> thgrp
http://www.rubycentral.com/book/ref_c_threadgroup.html (1 of 2) [7/20/2001 9:49:59 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Adds the given thread to this group, removing it from any other group to which it may have previously belonged. puts "Initial group is #{ThreadGroup::Default.list}" tg = ThreadGroup.new t1 = Thread.new { sleep 10 } t2 = Thread.new { sleep 10 } puts "t1 is #{t1}" puts "t2 is #{t2}" tg.add( t1 ) puts "Initial group now #{ThreadGroup::Default.list}" puts "tg group now #{tg.list}" produces: Initial group is # t1 is # t2 is # Initial group now ## tg group now #
list
thgrp.list -> anArray Returns an array of all existing Thread objects that belong to this group. ThreadGroup::Default.list
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: at gm local mktime new now times utc + -- asctime ctime day gmt? gmtime hour isdst localtime mday min mon month sec strftime to_a to_f to_i to_s tv_sec tv_usec usec utc utc? wday yday year zone Time is an abstraction of dates and times. Time is stored internally as the number of seconds and microseconds since the epoch, January 1, 1970 00:00 UTC. Also see the library modules Date and ParseDate, documented beginning on pages 443 and 457, respectively. The Time class treats GMT (Greenwich Mean Time) and UTC (Coordinated Universal Time)[Yes, UTC really does stand for Coordinated Universal Time. There was a committee involved.] as equivalent. GMT is the older way of referring to these baseline times but persists in the names of calls on Posix systems. All times are stored with some number of microseconds. Be aware of this fact when comparing times with each other---times that are apparently equal when displayed may be different when compared.
mixins Comparable:
, between?
class methods at
Time.at( aTime ) -> aTime Time.at( seconds [, microseconds] ) -> aTime Creates a new time object with the value given by aTime, or the given number of seconds (and optional microseconds) from epoch. Time.at(0) Time.at(946702800)
» Wed Dec 31 18:00:00 CST 1969 » Fri Dec 31 23:00:00 CST 1999
http://www.rubycentral.com/book/ref_c_time.html (1 of 9) [7/20/2001 9:50:00 PM]
Programming Ruby: The Pragmatic Programmer's Guide
gm
Time.gm( year [, month, day, hour, min, sec, usec] ) -> aTime Time.gm( sec, min, hour, day, month, year, wday, yday, isdst, tz ) -> aTime
Creates a time based on given values, interpreted as UTC (GMT). The year must be specified. Other values default to the minimum value for that field (and may be nil or omitted). Months may be specified by numbers from 1 to 12, or by the three-letter English month names. Hours are specified on a 24-hour clock (0..23). Raises an ArgumentError if any values are out of range. Will also accept ten arguments in the order output by Time#to_a. Time.gm(2000,"jan",1,20,15,1) » Sat Jan 01 20:15:01 UTC 2000
local
Time.local( year [, month, day, hour, min, sec, usec] ) -> aTime Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz ) -> aTime Same as Time::gm, but interprets the values in the local time zone. Time.local(2000,"jan",1,20,15,1) » Sat Jan 01 20:15:01 CST 2000
mktime
Time.mktime( year, month, day, hour, min, sec, usec ) -> aTime Synonym for Time::local.
new
Time.new -> aTime Returns a Time object initialized to the current system time. Note: The object created will be created using the resolution available on your system clock, and so may include fractional seconds. a = Time.new b = Time.new a == b "%.6f" % a.to_f "%.6f" % b.to_f
now
» » » » »
Sun Mar 04 23:30:26 CST 2001 Sun Mar 04 23:30:26 CST 2001 false "983770226.132865" "983770226.133372"
Time.now -> aTime Synonym for Time::new.
times
Time.times -> aStructTms
http://www.rubycentral.com/book/ref_c_time.html (2 of 9) [7/20/2001 9:50:00 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns a Tms structure (see Struct::Tms on page 388) that contains user and system CPU times for this process. t = Time.times [ t.utime, t.stime ]
utc
» [0.0, 0.01]
Time.utc( year [, month, day, hour, min, sec, usec] ) -> aTime Time.utc( sec, min, hour, day, month, year, wday, yday, isdst, tz ) -> aTime Synonym for Time::gm. Time.utc(2000,"jan",1,20,15,1) » Sat Jan 01 20:15:01 UTC 2000
instance methods +
time + aNumeric -> aTime Addition---Adds some number of seconds (possibly fractional) to time and returns that value as a new time. » Sun Mar 04 23:30:26 CST 2001 » Mon Mar 05 23:30:26 CST 2001
t = Time.now t + (60 * 60 * 24)
--
time - aTime -> aFloat time - aNumeric -> aTime Difference---Returns a new time that represents the difference between two times, or subtracts the given number of seconds in aNumeric from time. t = Time.now t2 = t + 2592000 t2 - t t2 - 2592000
» » » »
Sun Mar 04 23:30:26 CST 2001 Wed Apr 04 00:30:26 CDT 2001 2592000.0 Sun Mar 04 23:30:26 CST 2001
time anOtherTime -> -1, 0, +1 time aNumeric -> -1, 0, +1 Comparison---Compares time with anOtherTime or with aNumeric, which is the number of seconds (possibly fractional) since epoch. t = Time.now t2 = t + 2592000 t t2 t2 t t t
» » » » »
Sun Mar 04 23:30:26 CST 2001 Wed Apr 04 00:30:26 CDT 2001 -1 1 0
http://www.rubycentral.com/book/ref_c_time.html (3 of 9) [7/20/2001 9:50:00 PM]
Programming Ruby: The Pragmatic Programmer's Guide
asctime
time.asctime -> aString Returns a canonical string representation of time. » "Sun Mar
Time.now.asctime
ctime
4 23:30:26 2001"
time.ctime -> aString Synonym for Time#asctime.
day
time.day -> aFixnum Returns the day of the month (1..n) for time. t = Time.now t.day
gmt?
» Sun Mar 04 23:30:26 CST 2001 » 4
time.gmt? -> true or false Returns true if time represents a time in UTC (GMT). t = Time.now t.gmt? t = Time.gm(2000,"jan",1,20,15,1) t.gmt?
gmtime
» » » »
Sun Mar 04 23:30:26 CST 2001 false Sat Jan 01 20:15:01 UTC 2000 true
time.gmtime -> time Converts time to UTC (GMT), modifying the receiver. t = Time.now t.gmt? t.gmtime t.gmt?
hour
» » » »
Sun Mar 04 23:30:26 CST 2001 false Mon Mar 05 05:30:26 UTC 2001 true
time.hour -> aFixnum Returns the hour of the day (0..23) for time. t = Time.now t.hour
isdst
» Sun Mar 04 23:30:26 CST 2001 » 23
time.isdst -> true or false
http://www.rubycentral.com/book/ref_c_time.html (4 of 9) [7/20/2001 9:50:00 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns true if time occurs during Daylight Saving Time in its time zone. t = Time.local(2000, 7, 1) » » t.isdst t2 = Time.local(2000, 1, 1) » » t2.isdst
localtime
Sat Jul 01 00:00:00 CDT 2000 true Sat Jan 01 00:00:00 CST 2000 false
time.localtime -> time Converts time to local time (using the local time zone in effect for this process) modifying the receiver. t = Time.gm(2000, » t.gmt? » t.localtime » t.gmt?
mday
"jan", 1, 20, 15, 1) true Sat Jan 01 14:15:01 CST 2000 false
time.mday -> aFixnum Synonym for Time#day.
min
time.min -> aFixnum Returns the minute of the hour (0..59) for time. t = Time.now t.min
mon
» Sun Mar 04 23:30:26 CST 2001 » 30
time.mon -> aFixnum Returns the month of the year (1..12) for time. t = Time.now t.mon
month
» Sun Mar 04 23:30:26 CST 2001 » 3
time.month -> aFixnum Synonym for Time#mon.
sec
time.sec -> aFixnum
http://www.rubycentral.com/book/ref_c_time.html (5 of 9) [7/20/2001 9:50:00 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns the second of the minute (0..60)[Yes, seconds really can range from zero to 60. This allows the system to inject leap seconds every now and then to correct for the fact that years are not really a convenient number of hours long.] for time. t = Time.now t.sec
strftime
» Sun Mar 04 23:30:26 CST 2001 » 26
time.strftime( aString ) -> aString Formats time according to the directives in the given format string. See Table 22.9 on page 402 for the available values. Any text not listed as a directive will be passed through to the output string. t = Time.now t.strftime("Printed on %m/%d/%Y") » "Printed on 03/04/2001" » "at 11:30PM" t.strftime("at %I:%M%p") Time#strftime directives Format %a %A %b %B %c %d %H %I %j %m %M %p %S %U %W %w %x %X %y %Y %Z %%
Meaning The abbreviated weekday name (``Sun'') The full weekday name (``Sunday'') The abbreviated month name (``Jan'') The full month name (``January'') The preferred local date and time representation Day of the month (01..31) Hour of the day, 24-hour clock (00..23) Hour of the day, 12-hour clock (01..12) Day of the year (001..366) Month of the year (01..12) Minute of the hour (00..59) Meridian indicator (``AM'' or ``PM'') Second of the minute (00..60) Week number of the current year, starting with the first Sunday as the first day of the first week (00..53) Week number of the current year, starting with the first Monday as the first day of the first week (00..53) Day of the week (Sunday is 0, 0..6) Preferred representation for the date alone, no time Preferred representation for the time alone, no date Year without a century (00..99) Year with century Time zone name Literal ``%'' character
http://www.rubycentral.com/book/ref_c_time.html (6 of 9) [7/20/2001 9:50:00 PM]
Programming Ruby: The Pragmatic Programmer's Guide
to_a
time.to_a -> anArray Returns a ten-element anArray of values for time: {[ sec, min, hour, day, month, year, wday, yday, isdst, zone ]}. See the individual methods for an explanation of the valid ranges of each value. The ten elements can be passed directly to Time::utc or Time::local to create a new Time. now = Time.now » Sun Mar 04 23:30:26 CST 2001 » [26, 30, 23, 4, 3, 2001, 0, 63, false, "CST"] t = now.to_a
to_f
time.to_f -> aFloat Returns the value of time as a floating point number of seconds since epoch. t = Time.now "%10.5f" % t.to_f t.to_i
to_i
» "983770226.64529" » 983770226
time.to_i -> anInteger Returns the value of time as an integer number of seconds since epoch. t = Time.now "%10.5f" % t.to_f t.to_i
to_s
» "983770226.67497" » 983770226
time.to_s -> aString Returns a string representing time. Equivalent to calling Time#strftime with a format string of ``%a %b %d %H:%M:%S %Z %Y''. Time.now.to_s
tv_sec
» "Sun Mar 04 23:30:26 CST 2001"
time.tv_sec -> anInteger Synonym for Time#to_i.
tv_usec
time.tv_usec -> anInteger Synonym for Time#usec.
usec
time.usec -> anInteger
http://www.rubycentral.com/book/ref_c_time.html (7 of 9) [7/20/2001 9:50:00 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns just the number of microseconds for time. » Sun Mar 04 23:30:26 CST 2001 » "983770226.729373" » 729373
t = Time.now "%10.6f" % t.to_f t.usec
utc
time.utc -> time Synonym for Time#gmtime. t = Time.now t.utc? t.utc t.utc?
utc?
» » » »
Sun Mar 04 23:30:26 CST 2001 false Mon Mar 05 05:30:26 UTC 2001 true
time.utc? -> true or false Returns true if time represents a time in UTC (GMT). t = Time.now t.utc? t = Time.gm(2000,"jan",1,20,15,1) t.utc?
wday
» » » »
Sun Mar 04 23:30:26 CST 2001 false Sat Jan 01 20:15:01 UTC 2000 true
time.wday -> aFixnum Returns an integer representing the day of the week, 0..6, with Sunday == 0. t = Time.now t.wday
yday
» Sun Mar 04 23:30:26 CST 2001 » 0
time.yday -> aFixnum Returns an integer representing the day of the year, 1..366. t = Time.now t.yday
year
» Sun Mar 04 23:30:26 CST 2001 » 63
time.year -> aFixnum Returns the year for time (including the century). t = Time.now t.year
» Sun Mar 04 23:30:26 CST 2001 » 2001
http://www.rubycentral.com/book/ref_c_time.html (8 of 9) [7/20/2001 9:50:00 PM]
Programming Ruby: The Pragmatic Programmer's Guide
zone
time.zone -> aString Returns the name of the time zone used for time. t = Time.gm(2000, "jan", 1, 20, 15, 1) » t.zone "GMT" t = Time.local(2000, "jan", 1, 20, 15, 1) » t.zone "CST"
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Parent: Object Version: 1.6
Index: &^| The global value true is the only instance of class TrueClass and represents a logically true value in boolean expressions. The class provides operators allowing true to be used in logical expressions.
instance methods &
true & anObject -> anObject And---Returns false if anObject is nil or false, true otherwise.
^
true ^ anObject -> !anObject Exclusive Or---Returns true if anObject is nil or false, false otherwise.
|
true | anObject -> true
http://www.rubycentral.com/book/ref_c_trueclass.html (1 of 2) [7/20/2001 9:50:01 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Or---Returns true. As anObject is an argument to a method call, it is always evaluated; there is no short-circuit evaluation in this case. true | puts("or") true || puts("logical or") produces: or
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Relies on:
Index: Comparisons between? The Comparable mixin is used by classes whose objects may be ordered. The class must define the operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses to implement the conventional comparison operators () and the method between?. class SizeMatters include Comparable attr :str def (anOther) str.size anOther.str.size end def initialize(str) @str = str end def inspect @str end end s1 s2 s3 s4 s5
= = = = =
SizeMatters.new("Z") SizeMatters.new("YY") SizeMatters.new("XXX") SizeMatters.new("WWWW") SizeMatters.new("VVVVV")
s1 < s2 s4.between?(s1, s3)
» true » false
http://www.rubycentral.com/book/ref_m_comparable.html (1 of 2) [7/20/2001 9:50:01 PM]
Programming Ruby: The Pragmatic Programmer's Guide
s4.between?(s3, s5) [ s3, s2, s5, s4, s1 ].sort
» true » [Z, YY, XXX, WWWW, VVVVV]
instance methods Comparisons
anObject < otherObject -> true or false anObject true or false anObject == otherObject -> true or false anObject >= otherObject -> true or false anObject > otherObject -> true or false Compares two objects based on the receiver's method.
between?
anObject.between?( min, max ) -> true or false Returns false if anObject min is less than zero or if anObject max is greater than zero, true otherwise. 3.between?(1, 5) 6.between?(1, 5) 'cat'.between?('ant', 'dog') 'gnu'.between?('ant', 'dog')
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Relies on: each,
Index: collect detect each_with_index entries find find_all grep include? map max member? min reject select sort to_a The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection. If Enumerable#max, #min, or #sort is used, the objects in the collection must also implement a meaningful operator, as these methods rely on an ordering between members of the collection.
instance methods collect
enumObj.collect {| obj | block } -> anArray Returns a new array with the results of running block once for every element in enumObj. (1..4).collect {|i| i*i } (1..4).collect { "cat" }
detect
» [1, 4, 9, 16] » ["cat", "cat", "cat", "cat"]
enumObj.detect {| obj | block } -> anObject or nil Passes each entry in enumObj to block. Returns the first for which block is not false. Returns nil if no object matches. (1..10).detect {|i| i % 5 == 0 and i % 7 == 0 } (1..100).detect {|i| i % 5 == 0 and i % 7 == 0 }
each_with_index
» nil » 35
enumObj.each_with_index {| obj, i | block } -> nil Calls block with two arguments, the item and its index, for each item in enumObj. hash = Hash.new %w(cat dog wombat).each_with_index {|item, index| hash[item] = index } » {"dog"=>1, "wombat"=>2, "cat"=>0} hash
http://www.rubycentral.com/book/ref_m_enumerable.html (1 of 4) [7/20/2001 9:50:02 PM]
Programming Ruby: The Pragmatic Programmer's Guide
entries
enumObj.entries -> anArray Synonym for Enumerable#to_a.
find
enumObj.find {| obj | block } -> anObject or nil Synonym for Enumerable#detect.
find_all
enumObj.find_all {| obj | block } -> anArray Returns an array containing all elements of enumObj for which block is not false (see also Enumerable#reject). (1..10).find_all {|i|
grep
i % 3 == 0 }
» [3, 6, 9]
enumObj.grep( pattern ) -> anArray enumObj.grep( pattern ) {| obj | block } -> anArray Returns an array of every element in enumObj for which Pattern === element. If the optional block is supplied, each matching element is passed to it, and the block's result is stored in the output array. (1..100).grep 38..44 c = IO.constants c.grep(/SEEK/) res = c.grep(/SEEK/) res
include?
» [38, 39, 40, 41, 42, 43, 44] » ["SEEK_END", "SEEK_CUR", "SEEK_SET"] {|v| IO.const_get(v) } » [2, 1, 0]
enumObj.include?( anObject ) -> true or false Returns true if any member of enumObj equals anObject. Equality is tested using ==. IO.constants.include? "SEEK_SET" IO.constants.include? "SEEK_NO_FURTHER"
map
» true » false
enumObj.map {| obj | block } -> anArray Synonym for Enumerable#collect.
max
enumObj.max -> anObject enumObj.max {| a,b | block } -> anObject Returns the object in enumObj with the maximum value. The first form assumes all objects implement Comparable; the second uses the block to return a b. a = %w(albatross dog horse) a.max a.max {|a,b| a.length b.length }
http://www.rubycentral.com/book/ref_m_enumerable.html (2 of 4) [7/20/2001 9:50:02 PM]
» "horse" » "albatross"
Programming Ruby: The Pragmatic Programmer's Guide
member?
enumObj.member?( anObject ) -> true or false Synonym for Enumerable#include?.
min
enumObj.min -> anObject enumObj.min {| a,b | block } -> anObject Returns the object in enumObj with the minimum value. The first form assumes all objects implement Comparable; the second uses the block to return a b. a = %w(albatross dog horse) a.min a.min {|a,b| a.length b.length }
reject
» "albatross" » "dog"
enumObj.reject {| obj | block } -> anArray Returns an array for all elements of enumObj for which block is false (see also Enumerable#find_all). (1..10).reject {|i|
select
i % 3 == 0 } » [1, 2, 4, 5, 7, 8, 10]
enumObj.select {| obj | block } -> anArray Synonym for Enumerable#find_all.
sort
enumObj.sort -> anArray enumObj.sort {| a, b | block } -> anArray Returns an array containing the items in enumObj sorted, either according to their own method, or by using the results of the supplied block. The block should return -1, 0, or +1 depending on the comparison between a and b. » ["flea", "kea", "rhea"] %w(rhea kea flea).sort (1..10).sort {|a,b| b a} » [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] The following code sorts some files on modification time. files = Dir["*"] sorted = files.sort {|a,b| File.new(a).mtime File.new(b).mtime} » ["mon", "tues", "wed", "thurs"] sorted This sort is inefficient: it generates two new File objects during every comparison. A slightly better technique is to use the Kernel#test method to generate the modification times directly. files = Dir["*"] sorted = files.sort { |a,b| test(?M, a) test(?M, b) } » ["mon", "tues", "wed", "thurs"] sorted This still generates many unnecessary Time objects. A more efficient technique is to cache the sort keys (modification times in this case) before the sort. Perl users often call this approach a Schwartzian Transform, after Randal Schwartz. We construct a temporary array, where each element is an array containing our sort key along with the filename. We sort this array, and then extract the filename from
http://www.rubycentral.com/book/ref_m_enumerable.html (3 of 4) [7/20/2001 9:50:02 PM]
Programming Ruby: The Pragmatic Programmer's Guide
the result. sorted = Dir["*"].collect { |f| [test(?M, f), f] }.sort.collect { |f| f[1] } » ["mon", "tues", "wed", "thurs"] sorted
to_a
enumObj.to_a -> anArray Returns an array containing the items in enumObj. » [1, 2, 3, 4, 5, 6, 7] (1..7).to_a { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a » [["a", 1], ["b", 2], ["c", 3]]
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
module Errno Index: Ruby exception objects are subclasses of Exception. However, operating systems typically report errors using plain integers. Module Errno is created dynamically to map these operating system errors to Ruby classes, with each error number generating its own subclass of SystemCallError. As the subclass is created in module Errno, its name will start Errno::. The names of the Errno:: classes depend on the environment in which Ruby runs. On a typical Unix or Windows platform, there are Errno classes such as Errno::EACCES, Errno::EAGAIN, Errno::EINTR, and so on. The integer operating system error number corresponding to a particular error is available as the class constant Errno::error::Errno. » » »
Errno::EACCES::Errno Errno::EAGAIN::Errno Errno::EINTR::Errno
13 11 4
The full list of operating system errors on your particular platform are available as the constants of Errno. Errno.constants » E2BIG, EACCES, EADDRINUSE, EADDRNOTAVAIL, EADV, EAFNOSUPPORT, EAGAIN, ... Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
module FileTest Index: blockdev? chardev? directory? executable? executable_real? exist? exists? file? grpowned? owned? pipe? readable? readable_real? setgid? setuid? size size? socket? sticky? symlink? writable? writable_real? zero? FileTest implements file test operations similar to those used in File::Stat.
instance methods blockdev?
FileTest.blockdev?( aString ) -> true or false Returns true if the named file is a block device, false if it isn't or if the operating system doesn't support this feature. FileTest.blockdev?( "testfile" )
chardev?
» false
FileTest.chardev?( aString ) -> true or false Returns true if the named file is a character device, false if it isn't or if the operating system doesn't support this feature. FileTest.chardev?( "/dev/tty" )
directory?
» true
FileTest.directory?( aString ) -> true or false Returns true if this named file is a directory, false otherwise. FileTest.directory?( "." )
executable?
FileTest.executable?( aString ) -> true or false
http://www.rubycentral.com/book/ref_m_filetest.html (1 of 4) [7/20/2001 9:50:03 PM]
» true
Programming Ruby: The Pragmatic Programmer's Guide
Returns true if the named file is executable. The tests are made using the effective owner of the process. FileTest.executable?( "testfile" )
executable_real?
» false
FileTest.executable_real?( aString ) -> true or false Same as FileTest#executable?, but tests using the real owner of the process.
exist?
FileTest.exist? ( aString ) -> true or false Returns true if the named file exists. FileTest.exist?( "testfile" )
exists?
» true
FileTest.exists? ( aString ) -> true or false Synonym for FileTest::exist?.
file?
FileTest.file?( aString ) -> true or false Returns true if the named file is a regular file (not a device file, pipe, socket, etc.). FileTest.file?( "testfile" )
grpowned?
» true
FileTest.grpowned?( aString ) -> true or false Returns true if the effective group id of the process is the same as the group id of the named file. On Windows NT, returns false. FileTest.grpowned?( "/etc/passwd" )
owned?
» false
FileTest.owned?( aString ) -> true or false Returns true if the effective user id of the process is the same as the owner of the named file. FileTest.owned?( "/etc/passwd" )
pipe?
» false
FileTest.pipe?( aString ) -> true or false Returns true if the operating system supports pipes and the named file is a pipe, false otherwise. FileTest.pipe?( "testfile" )
readable?
FileTest.readable?( aString ) -> true or false
http://www.rubycentral.com/book/ref_m_filetest.html (2 of 4) [7/20/2001 9:50:03 PM]
» false
Programming Ruby: The Pragmatic Programmer's Guide
Returns true if the named file is readable by the effective user id of this process. FileTest.readable?( "testfile" )
readable_real?
» true
FileTest.readable_real?( aString ) -> true or false Returns true if the named file is readable by the real user id of this process. FileTest.readable_real?( "testfile" )
setgid?
» true
FileTest.setgid?( aString ) -> true or false Returns true if the named file's set-group-id permission bit is set, and false if it isn't or if the operating system doesn't support this feature. FileTest.setgid?( "/usr/sbin/lpc" )
setuid?
» true
FileTest.setuid?( aString ) -> true or false Returns true if the named file's set-user-id permission bit is set, and false if it isn't or if the operating system doesn't support this feature. FileTest.setuid?( "/bin/su" )
size
» true
FileTest.size( aString ) -> anInteger Returns the size of the named file in bytes. FileTest.size( "testfile" )
size?
» 66
FileTest.size?( aString ) -> aFixnum or nil Returns nil if the named file is of zero length; otherwise, returns a nonzero aFixnum. FileTest.size?( "testfile" ) FileTest.size?( "/dev/zero" )
socket?
» 66 » nil
FileTest.socket?( aString ) -> true or false Returns true if the named file is a socket, false if it isn't or if the operating system doesn't support this feature.
sticky?
FileTest.sticky?( aString ) -> true or false Returns true if the named file has its sticky bit set, false if it doesn't or if the operating system doesn't support this feature.
http://www.rubycentral.com/book/ref_m_filetest.html (3 of 4) [7/20/2001 9:50:03 PM]
Programming Ruby: The Pragmatic Programmer's Guide
symlink?
FileTest.symlink?( aString ) -> true or false
Returns true if the named file is a symbolic link, false if it isn't or if the operating system doesn't support this feature.
writable?
FileTest.writable?( aString ) -> true or false Returns true if the named file is writable by the effective user id of this process.
writable_real?
FileTest.writable_real?( aString ) -> true or false Returns true if the named file is writable by the real user id of this process.
zero?
FileTest.zero?( aString ) -> true or false Returns true if the named file is of zero length, false otherwise.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
module GC Index: disable enable start garbage_collect The GC module provides an interface to Ruby's mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module, described beginning on page 434.
class methods disable
GC.disable -> true or false Disables garbage collection, returning true if garbage collection was already disabled. GC.disable GC.disable
enable
» »
false true
GC.enable -> true or false Enables garbage collection, returning true if garbage collection was disabled. GC.disable GC.enable GC.enable
start
» » »
false true false
GC.start -> nil Initiates garbage collection, unless manually disabled. GC.start
http://www.rubycentral.com/book/ref_m_gc.html (1 of 2) [7/20/2001 9:50:03 PM]
»
nil
Programming Ruby: The Pragmatic Programmer's Guide
instance methods garbage_collect
garbage_collect -> nil Equivalent to GC::start. include GC garbage_collect
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
module Math Index: atan2 cos exp frexp ldexp log log10 sin sqrt tan The Math module contains module functions for basic trigonometric and transcendental functions.
constants E
2.718281828 Value of e (base of natural logarithms)
PI 3.141592654 Value of PI
class methods atan2
Math.atan2( y, x ) -> aFloat Computes the arc tangent given y and x. Returns -PI..PI.
cos
Math.cos( aNumeric ) -> aFloat Computes the cosine of aNumeric (expressed in radians). Returns -1..1.
exp
Math.exp( aNumeric ) -> aFloat Returns e raised to the power of aNumeric.
http://www.rubycentral.com/book/ref_m_math.html (1 of 2) [7/20/2001 9:50:04 PM]
Programming Ruby: The Pragmatic Programmer's Guide
frexp
Math.frexp( aNumeric ) -> anArray Returns a two-element array ([aFloat, aFixnum]) containing the normalized fraction and exponent of aNumeric.
ldexp
Math.ldexp( aFloat, anInteger ) -> aFloat Returns the value of aFloat *2anInteger.
log
Math.log( aNumeric ) -> aFloat Returns the natural logarithm of aNumeric.
log10
Math.log10( aNumeric ) -> aFloat Returns the base 10 logarithm of aNumeric.
sin
Math.sin( aNumeric ) -> aFloat Computes the sine of aNumeric (expressed in radians). Returns -1..1.
sqrt
Math.sqrt( aNumeric ) -> aFloat Returns the non-negative square root of aNumeric. Raises ArgError if aNumeric is less than zero.
tan
Math.tan( aNumeric ) -> aFloat Returns the tangent of aNumeric (expressed in radians).
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Object-Oriented Design Libraries One of the interesting things about Ruby is the way it blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. ● The Visitor pattern (Design Patterns, ) is a way of traversing a collection without having to know the internal organization of that collection. ● Delegation is a way of composing classes more flexibly and dynamically than can be done using standard inheritance. ● The Singleton pattern is a way of ensuring that only one instantiation of a particular class exists at a time. ● The Observer pattern implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred. Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently. Before we get into the proper library descriptions, let's get the simplest strategy out of the way.
The Visitor Pattern It's the method each.
Library: delegate Object delegation is a way of composing objects---extending an object with the capabilities of another---at runtime. This promotes writing flexible, decoupled code, as there are no compile-time dependencies between the users of the overall class and the delegates. The Ruby Delegator class implements a simple but powerful delegation scheme, where requests are
http://www.rubycentral.com/book/lib_patterns.html (1 of 6) [7/20/2001 9:50:04 PM]
Programming Ruby: The Pragmatic Programmer's Guide
automatically forwarded from a master class to delegates or their ancestors, and where the delegate can be changed at runtime with a single method call. The delegate.rb library provides two mechanisms for allowing an object to forward messages to a delegate. 1. For simple cases where the class of the delegate is fixed, arrange for the master class to be a subclass of DelegateClass, passing the name of the class to be delegated as a parameter (Example 1). Then, in your class's initialize method, you'd call the superclass, passing in the object to be delegated. For example, to declare a class Fred that also supports all the methods in Flintstone, you'd write class Fred < DelegateClass(Flintstone) def initialize # ... super(Flintstone.new(...)) end # ... end This is subtly different from using subclassing. With subclassing, there is only one object, which has the methods and the defined class, its parent, and their ancestors. With delegation there are two objects, linked so that calls to one may be delegated to the other. 2. For cases where the delegate needs to be dynamic, make the master class a subclass of SimpleDelegator (Example 2). You can also add delegation capabilities to an existing object using SimpleDelegator (Example 3). In these cases, you can call the __setobj__ method in SimpleDelegator to change the object being delegated at runtime. Example 1. Use the DelegateClass method and subclass the result when you need a class with its own behavior that also delegates to an object of another class. In this example, we assume that the @sizeInInches array is large, so we want only one copy of it. We then define a class that accesses it, converting the values to feet. require 'delegate' sizeInInches = [ 10, 15, 22, 120 ] class Feet < DelegateClass(Array) def initialize(arr) super(arr) end def [](*n) val = super(*n) case val.type when Numeric; val/12.0 else; val.collect {|i| i/12.0} end http://www.rubycentral.com/book/lib_patterns.html (2 of 6) [7/20/2001 9:50:04 PM]
Programming Ruby: The Pragmatic Programmer's Guide
end end sizeInFeet = Feet.new(sizeInInches) sizeInInches[0..3] » [10, 15, 22, 120] » [0.8333333333, 1.25, 1.833333333, 10.0] sizeInFeet[0..3] Example 2. Use subclass SimpleDelegator when you want an object that both has its own behavior and delegates to different objects during its lifetime. This is an example of the State pattern. Objects of class TicketOffice sell tickets if a seller is available, or tell you to come back tomorrow if there is no seller. require 'delegate' class TicketSeller def sellTicket() return 'Here is a ticket' end end class NoTicketSeller def sellTicket() "Sorry-come back tomorrow" end end class TicketOffice < SimpleDelegator def initialize @seller = TicketSeller.new @noseller = NoTicketSeller.new super(@seller) end def allowSales(allow = true) __setobj__(allow ? @seller : @noseller) allow end end to = TicketOffice.new » "Here is a ticket" to.sellTicket » false to.allowSales(false) » "Sorry-come back tomorrow" to.sellTicket » true to.allowSales(true) » "Here is a ticket" to.sellTicket
http://www.rubycentral.com/book/lib_patterns.html (3 of 6) [7/20/2001 9:50:04 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Example 3. Create SimpleDelegator objects when you want a single object to delegate all its methods to two or more other objects. # Example 3 - delegate from existing object seller = TicketSeller.new noseller = NoTicketSeller.new to = SimpleDelegator.new(seller) » "Here's a ticket" to.sellTicket » "Here's a ticket" to.sellTicket to.__setobj__(noseller) » "Sorry-come back tomorrow" to.sellTicket to.__setobj__(seller) » "Here's a ticket" to.sellTicket
Library: observer The Observer pattern, also known as Publish/Subscribe, provides a simple mechanism for one object to inform a set of interested third-party objects when its state changes. In the Ruby implementation, the notifying class mixes in the Observable module, which provides the methods for managing the associated observer objects. add_observer(obj) delete_observer(obj)
Add obj as an observer on this object. obj will now receive notifications. Delete obj as an observer on this object. It will no longer receive notifications. delete_observers Delete all observers associated with this object. count_observers Return the count of observers associated with this object. changed(newState=true) Set the changed state of this object. Notifications will be sent only if the changed state is true. changed? Query the changed state of this object. notify_observers(*args) If this object's changed state is true, invoke the update method in each currently associated observer in turn, passing it the given arguments. The changed state is then set to false. The observers must implement the update method to receive notifications.
http://www.rubycentral.com/book/lib_patterns.html (4 of 6) [7/20/2001 9:50:04 PM]
Programming Ruby: The Pragmatic Programmer's Guide
require "observer" class Ticker # Periodically fetch a stock price include Observable def initialize(symbol) @symbol = symbol end def run lastPrice = nil loop do price = Price.fetch(@symbol) print "Current price: #{price}\n" if price != lastPrice changed # notify observers lastPrice = price notify_observers(Time.now, price) end end end end class Warner def initialize(ticker, limit) @limit = limit ticker.add_observer(self) # all warners are observers end end class WarnLow < Warner def update(time, price) # callback for observer if price < @limit print "--- #{time.to_s}: Price below #@limit: #{price}\n" end end end class WarnHigh < Warner def update(time, price) # callback for observer if price > @limit print "+++ #{time.to_s}: Price above #@limit: #{price}\n" end http://www.rubycentral.com/book/lib_patterns.html (5 of 6) [7/20/2001 9:50:04 PM]
Programming Ruby: The Pragmatic Programmer's Guide
end end ticker = Ticker.new("MSFT") WarnLow.new(ticker, 80) WarnHigh.new(ticker, 120) ticker.run produces: Current Current --- Sun Current Current +++ Sun Current Current Current --- Sun
price: price: Mar 04 price: price: Mar 04 price: price: price: Mar 04
83 75 23:26:31 CST 2001: Price below 80: 75 90 134 23:26:31 CST 2001: Price above 120: 134 134 112 79 23:26:31 CST 2001: Price below 80: 79
Library: singleton The Singleton design pattern ensures that only one instance of a particular class may be created. The singleton library makes this simple to implement. Mix the Singleton module into each class that is to be a singleton, and that class's new method will be made private. In its place, users of the class call the method instance, which returns a singleton instance of that class. In this example, the two instances of MyClass are the same object. require 'singleton' class MyClass include Singleton end a = MyClass.instance b = MyClass.instance
Previous
» #<MyClass:0x4018c924> Contents ^
Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide" Copyright © 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0. This reference is available for download.
http://www.rubycentral.com/book/lib_patterns.html (6 of 6) [7/20/2001 9:50:04 PM]
Next >
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Network and Web Libraries Ruby provides two levels of access to network services. At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols. These are documented in the next section. Ruby also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on. These are documented starting on page 486. Finally, the CGI libraries, documented beginning on page 501, provide server-side developers with a convenient interface for developing Web applications.
Socket-Level Access Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a process, between processes on the same machine, or between processes on different continents. Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on. The socket library provides specific classes for handling the common transports as well as a generic interface for handling the rest. All functionality in the socket library is accessible through a single extension library. Access it using require 'socket' Sockets have their own vocabulary: domain The family of protocols that will be used as the transport mechanism. These values are constants such as PF_INET, PF_UNIX, PF_X25, and so on. type The type of communications between the two endpoints, typically SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for connectionless protocols. protocol Typically zero, this may be used to identify a variant of a protocol within a domain and type. hostName The identifier of a network interface: ❍ a string, which can be a host name, a dotted-quad address, or an IPV6 address in colon (and possibly dot) notation, ❍ the string ``'', which specifies an INADDR_BROADCAST address, ❍ a zero-length string, which specifies INADDR_ANY, or ❍ an Integer, interpreted as a binary address in host byte order. port (sometimes called service) Each server listens for clients calling on one or more ports. A port may be a Fixnum port number, a string containing a port number, or the name of a service. Sockets are children of class IO. Once a socket has been successfully opened, the conventional I/O methods may be used. However, greater efficiency is sometimes obtained by using socket-specific methods. As with other I/O classes, socket I/O blocks by default. The hierarchy of the socket classes is shown in Figure 26.1 on page 475. For more information on the use of sockets, see your operating system documentation. You'll also find a comprehensive treatment in W. Richard Stevens, Unix Network Programming, Volumes 1 and 2 .
http://www.rubycentral.com/book/lib_network.html (1 of 32) [7/20/2001 9:50:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class BasicSocket
Parent: IO Version: 1.6
Index: do_not_reverse_lookup do_not_reverse_lookup= lookup_order lookup_order= close_read close_write getpeername getsockname getsockopt recv send setsockopt shutdown BasicSocket is an abstract base class for all other socket classes. This class and its subclasses often manipulate addresses using something called a struct sockaddr, which is effectively an opaque binary string.[In reality, it maps onto the underlying C-language struct sockaddr set of structures, documented in the man pages and in the books by Stevens.]
class methods do_not_reverse_lookup
BasicSocket.do_not_reverse_lookup -> true or false Returns the value of the global reverse lookup flag. If set to true, queries on remote addresses will return the numeric address but not the host name.
do_not_reverse_lookup=
BasicSocket.do_not_reverse_lookup = true or false Sets the global reverse lookup flag.
lookup_order
BasicSocket.lookup_order -> aFixnum Returns the global address lookup order, one of: Order LOOKUP_UNSP LOOKUP_INET LOOKUP_INET6
lookup_order=
Families Searched AF_UNSPEC AF_INET, AF_INET6, AF_UNSPEC AF_INET6, AF_INET, AF_UNSPEC
BasicSocket.lookup_order = aFixnum Sets the global address lookup order.
instance methods close_read
aSession.close_read -> nil Closes the readable connection on this socket.
close_write
aSession.close_write -> nil Closes the writable connection on this socket.
getpeername
aSession.getpeername -> aString Returns the struct sockaddr structure associated with the other end of this socket connection.
http://www.rubycentral.com/book/lib_network.html (2 of 32) [7/20/2001 9:50:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
getsockname
aSession.getsockname -> aString Returns the struct sockaddr structure associated with aSession.
getsockopt
aSession.getsockopt( level, optname ) -> aString Returns the value of the specified option.
recv
aSession.recv( len, [, flags] ) -> aString Receives up to len bytes from aSession.
send
aSession.send( aString, flags, [, to] ) -> aFixnum Sends aString over aSession. If specified, to is a struct sockaddr specifying the recipient address. flags are the sum or one or more of the MSG_ options (listed on page 482). Returns the number of characters sent.
setsockopt
aSession.setsockopt( level, optname, optval ) -> 0 Sets a socket option. level is one of the socket-level options (listed on page 482). optname and optval are protocol specific---see your system documentation for details.
shutdown
aSession.shutdown( how=2 ) -> 0 Shuts down the receive (how == 0), or send (how == 1), or both (how == 2), parts of this socket.
class IPSocket
Parent: BasicSocket Version: 1.6
Index: getaddress addr peeraddr Class IPSocket is a base class for sockets using IP as their transport. TCPSocket and UDPSocket are based on this class.
class methods getaddress
IPSocket.getaddress( hostName ) -> aString Returns the dotted-quad IP address of hostName. a = IPSocket.getaddress('www.ruby-lang.org') » "210.251.121.214" a
instance methods addr
aSession.addr -> anArray
http://www.rubycentral.com/book/lib_network.html (3 of 32) [7/20/2001 9:50:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns the domain, port, name, and IP address of aSession as a four-element array. The name will be returned as an address if the do_not_reverse_lookup flag is true. u = UDPSocket.new u.bind('localhost', 8765) u.addr » ["AF_INET", 8765, "localhost", "127.0.0.1"] BasicSocket.do_not_reverse_lookup = true u.addr » ["AF_INET", 8765, "127.0.0.1", "127.0.0.1"]
peeraddr
aSession.peeraddr -> anArray Returns the domain, port, name, and IP address of the peer.
class TCPSocket
Parent: IPSocket Version: 1.6
Index: gethostbyname new open recvfrom t = TCPSocket.new('localhost', 'ftp') t.gets » "220 zip.local.thomases.com FTP server (Version 6.2/OpenBSD/Linux-0.11) ready.\r\n" t.close » nil
class methods gethostbyname
TCPSocket.gethostbyname( hostName ) -> anArray Looks up hostName and returns its canonical name, an array containing any aliases, the address type (AF_INET), and the dotted-quad IP address. a = TCPSocket.gethostbyname('ns.pragprog.com') a » ["pragprog.com", [], 2, "63.68.129.131"]
new
TCPSocket.new( hostName, port ) -> aSession Opens a TCP connection to hostName on the port.
open
TCPSocket.open( hostName, port ) -> aSession Synonym for TCPSocket#new.
instance methods recvfrom
aSession.recvfrom( len [, flags] ) -> anArray Receives up to len bytes on the connection. flags is zero or more of the MSG_ options (listed on page 482). Returns a two-element array. The first element is the received data, the second is an array containing information about the peer. t = TCPSocket.new('localhost', 'ftp') data = t.recvfrom(30) data » ["220 zip.local.thomases.com FTP", ["AF_INET", 21, "localhost", "127.0.0.1"]]
http://www.rubycentral.com/book/lib_network.html (4 of 32) [7/20/2001 9:50:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class SOCKSSocket
Parent: TCPSocket Version: 1.6
Index: new open close Class SOCKSSocket supports connections based on the SOCKS protocol.
class methods new
SOCKSSocket.new( hostName, port ) -> aSession Opens a SOCKS connection to port on hostName.
open
SOCKSSocket.open( hostName, port ) -> aSession Synonym for SOCKSSocket#new.
instance methods close
aSession.close -> nil Closes this SOCKS connection.
class TCPServer
Parent: TCPSocket Version: 1.6
Index: new open accept A TCPServer accepts incoming TCP connections. Here is a Web server that listens on a given port and returns the time. require 'socket' port = (ARGV[0] || 80).to_i server = TCPServer.new('localhost', port) while (session = server.accept) puts "Request: #{session.gets}" session.print "HTTP/1.1 200/OK\r\nContent-type: text/html\r\n\r\n" session.print "#{Time.now}\r\n" session.close end
class methods new
TCPServer.new( [hostName,] port ) -> aSession
http://www.rubycentral.com/book/lib_network.html (5 of 32) [7/20/2001 9:50:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Creates a new socket on the given interface (identified by hostName and port). If hostName is omitted, the server will listen on all interfaces on the current host (equivalent to an address of 0.0.0.0).
open
TCPServer.open( [hostName,] port ) -> aSession Synonym for TCPServer#new.
instance methods accept
aSession.accept -> aTCPSocket Waits for a connection on aSession, and returns a new TCPSocket connected to the caller. See the example on page 478.
class UDPSocket
Parent: IPSocket Version: 1.6
Index: new open bind connect recvfrom send UDP sockets send and receive datagrams. In order to receive data, a socket must be bound to a particular port. You have two choices when sending data: you can connect to a remote UDP socket and thereafter send datagrams to that port, or you can specify a host and port for use with every packet you send. This example is a UDP server that prints the message it receives. It is called by both connectionless and connection-based clients. require 'socket' $port = 4321 sThread = Thread.start do # run server in a thread server = UDPSocket.open server.bind(nil, $port) 2.times { p server.recvfrom(64) } end # Ad-hoc client UDPSocket.open.send("ad hoc", 0, 'localhost', $port) # Connection based client sock = UDPSocket.open sock.connect('localhost', $port) sock.send("connection-based", 0) sThread.join produces: ["ad hoc", ["AF_INET", 1544, "localhost", "127.0.0.1"]] ["connection-based", ["AF_INET", 1545, "localhost", "127.0.0.1"]]
http://www.rubycentral.com/book/lib_network.html (6 of 32) [7/20/2001 9:50:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class methods new
UDPSocket.new( family = AF_INET ) -> aSession Creates an endpoint for UDP communications, optionally specifying the address family.
open
UDPSocket.open( family = AF_INET ) -> aSession Synonym for UDPSocket#new.
instance methods bind
aSession.bind( hostName, port ) -> 0 Associates the local end of the UDP connection with a given hostName and port. Must be used by servers to establish an accessible endpoint.
connect
aSession.connect( hostName, port ) -> 0 Creates a connection to the given hostName and port. Subsequent UDPSocket#send requests that don't override the recipient will use this connection. Multiple connect requests may be issued on aSession: the most recent will be used by send.
recvfrom
aSession.recvfrom( len [, flags] ) -> anArray Receives up to len bytes from aSession. flags is zero or more of the MSG_ options (listed on page 482). The result is a two-element array containing the received data and information on the sender. See the example on page 479.
send
aSession.send( aString, flags ) -> aFixnum aSession.send( aString, flags, hostName, port ) -> aFixnum The two-parameter form sends aString on an existing connection. The four-parameter form sends aString to port on hostName.
class UNIXSocket
Parent: BasicSocket Version: 1.6
Index: new open addr path peeraddr recvfrom Class UNIXSocket supports interprocess communications using the Unix domain protocol. Although the underlying protocol supports both datagram and stream connections, the Ruby library provides only a stream-based connection.
http://www.rubycentral.com/book/lib_network.html (7 of 32) [7/20/2001 9:50:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
require 'socket' $path = "/tmp/sample" sThread = Thread.start do sock = UNIXServer.open($path) s1 = sock.accept p s1.recvfrom(124) end
# run server in a thread
client = UNIXSocket.open($path) client.send("hello", 0) client.close sThread.join produces: ["hello", ["AF_UNIX", ""]]
class methods new
UNIXSocket.new( path ) -> aSession Opens a new domain socket on path, which must be a pathname.
open
UNIXSocket.open( path ) -> aSession Synonym for UNIXSocket#new.
instance methods addr
aSession.addr -> anArray Returns the address family and path of this socket.
path
aSession.path -> aString Returns the path of this domain socket.
peeraddr
aSession.peeraddr -> anArray Returns the address family and path of the server end of the connection.
recvfrom
aSession.recvfrom( len [, flags] ) -> anArray Receives up to len bytes from aSession. flags is zero or more of the MSG_ options (listed on page 482). The first element of the returned array is the received data, and the second contains (minimal) information on the sender.
http://www.rubycentral.com/book/lib_network.html (8 of 32) [7/20/2001 9:50:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class UNIXServer
Parent: UNIXSocket Version: 1.6
Index: new open accept Class UNIXServer provides a simple Unix domain socket server. See UNIXSocket for example code.
class methods new
UNIXServer.new( path ) -> aSession Creates a server on the given path. The corresponding file must not exist at the time of the call.
open
UNIXServer.open( path ) -> aSession Synonym for UNIXServer#new.
instance methods accept
aSession.accept -> aUnixSocket Waits for a connection on the server socket and returns a new socket object for that connection. See the example for UNIXSocket on page 480.
class Socket
Parent: BasicSocket Version: 1.6
Index: for_fd getaddrinfo gethostbyaddr gethostbyname gethostname getnameinfo getservbyname new open pair socketpair accept bind connect listen recvfrom Class Socket provides access to the underlying operating system socket implementation. It can be used to provide more operating system-specific functionality than the protocol-specific socket classes, but at the expense of greater complexity. In particular, the class handles addresses using struct sockaddr structures packed into Ruby strings, which can be a joy to manipulate.
constants Class Socket defines constants for use throughout the socket library. Individual constants are available only on architectures that support the related facility. Types: SOCK_DGRAM, SOCK_PACKET, SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET, SOCK_STREAM. Protocol families: http://www.rubycentral.com/book/lib_network.html (9 of 32) [7/20/2001 9:50:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
PF_APPLETALK, PF_AX25, PF_INET6, PF_INET, PF_IPX, PF_UNIX, PF_UNSPEC. Address families: AF_APPLETALK, AF_AX25, AF_INET6, AF_INET, AF_IPX, AF_UNIX, AF_UNSPEC. Lookup-order options: LOOKUP_INET6, LOOKUP_INET, LOOKUP_UNSPEC. Send/receive options: MSG_DONTROUTE, MSG_OOB, MSG_PEEK. Socket-level options: SOL_ATALK, SOL_AX25, SOL_IPX, SOL_IP, SOL_SOCKET, SOL_TCP, SOL_UDP. Socket options: SO_BROADCAST, SO_DEBUG, SO_DONTROUTE, SO_ERROR, SO_KEEPALIVE, SO_LINGER, SO_NO_CHECK, SO_OOBINLINE, SO_PRIORITY, SO_RCVBUF, SO_REUSEADDR, SO_SNDBUF, SO_TYPE. QOS options: SOPRI_BACKGROUND, SOPRI_INTERACTIVE, SOPRI_NORMAL. Multicast options: IP_ADD_MEMBERSHIP, IP_DEFAULT_MULTICAST_LOOP, IP_DEFAULT_MULTICAST_TTL, IP_MAX_MEMBERSHIPS, IP_MULTICAST_IF, IP_MULTICAST_LOOP, IP_MULTICAST_TTL. TCP options: TCP_MAXSEG, TCP_NODELAY. getaddrinfo error codes: EAI_ADDRFAMILY, EAI_AGAIN, EAI_BADFLAGS, EAI_BADHINTS, EAI_FAIL, EAI_FAMILY, EAI_MAX, EAI_MEMORY, EAI_NODATA, EAI_NONAME, EAI_PROTOCOL, EAI_SERVICE, EAI_SOCKTYPE, EAI_SYSTEM. ai_flags values: AI_ALL, AI_CANONNAME, AI_MASK, AI_NUMERICHOST, AI_PASSIVE, AI_V4MAPPED_CFG.
class methods for_fd
Socket.for_fd( anFD ) -> aSession Wraps an already open file descriptor into a socket object.
getaddrinfo
Socket.getaddrinfo( hostName, port, [family [socktype [protocol [flags]]]] ) -> anArray
http://www.rubycentral.com/book/lib_network.html (10 of 32) [7/20/2001 9:50:09 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns an array of arrays describing the given host and port (optionally qualified as shown). Each subarray contains the address family, port number, host name, host IP address, protocol family, socket type, and protocol. for line in Socket.getaddrinfo('www.microsoft.com', 'http') puts line.join(", ") end produces: AF_INET, AF_INET, AF_INET, AF_INET, AF_INET,
gethostbyaddr
80, 80, 80, 80, 80,
microsoft.net, microsoft.net, microsoft.com, microsoft.com, microsoft.net,
207.46.130.149, 2, 1, 6 207.46.131.137, 2, 1, 6 207.46.230.218, 2, 1, 6 207.46.230.219, 2, 1, 6 207.46.130.14, 2, 1, 6
Socket.gethostbyaddr( addr, type=AF_INET ) -> anArray Returns the host name, address family, and sockaddr component for the given address. a = Socket.gethostbyname("63.68.129.130") res = Socket.gethostbyaddr(a[3], a[2]) res.join(', ') » "somewhere.in.pragprog.com, , 2, ?D\201\202"
gethostbyname
Socket.gethostbyname( hostName ) -> anArray Returns a four-element array containing the canonical host name, a subarray of host aliases, the address family, and the address portion of the sockaddr structure. a = Socket.gethostbyname("63.68.129.130") a.join(', ') » "somewhere.in.pragprog.com, , 2, ?D\201\202"
gethostname
aSession.gethostname -> aString Returns the name of the current host.
getnameinfo
Socket.getnameinfo( addr [, flags] ) -> anArray Looks up the given address, which may be either a string containing a sockaddr or a three- or four-element array. If sockaddr is an array, it should contain the string address family, the port (or nil), and the host name or IP address. If a fourth element is present and not nil, it will be used as the host name. Returns a canonical hostname (or address) and port number as an array. a = Socket.getnameinfo(["AF_INET", '23', 'www.ruby-lang.org']) a » ["helium.ruby-lang.org", "telnet"]
getservbyname
Socket.getservbyname( service, proto='tcp' ) -> aFixnum Returns the port corresponding to the given service and protocol. Socket.getservbyname("telnet")
new
Socket.new( domain, type, protocol ) -> aSession Creates a socket using the given parameters.
open
Socket.open( domain, type, protocol ) -> aSession
http://www.rubycentral.com/book/lib_network.html (11 of 32) [7/20/2001 9:50:10 PM]
» 23
Programming Ruby: The Pragmatic Programmer's Guide
Synonym for Socket#new.
pair
Socket.pair( domain, type, protocol ) -> anArray Returns a pair of connected, anonymous sockets of the given domain, type, and protocol.
socketpair
Socket.socketpair( domain, type, protocol ) -> anArray Synonym for Socket#pair.
instance methods accept
aSession.accept -> anArray Accepts an incoming connection returning an array containing a new Socket object and a string holding the struct sockaddr information about the caller.
bind
aSession.bind( sockaddr ) -> 0 Binds to the given struct sockaddr, contained in a string.
connect
aSession.connect( sockaddr ) -> 0 Connects to the given struct sockaddr, contained in a string.
listen
aSession.listen( aFixnum ) -> 0 Listens for connections, using the specified aFixnum as the backlog.
recvfrom
aSession.recvfrom( len [, flags] ) -> anArray Receives up to len bytes from aSession. flags is zero or more of the MSG_ options. The first element of the result is the data received. The second element contains protocol-specific information on the sender.
Higher-Level Access Ruby provides a set of classes to facilitate writing clients for: ● File Transfer Protocol (FTP) ● HyperText Transfer Protocol (HTTP) ● Post Office Protocol (POP) ● Simple Mail Transfer Protocol (SMTP) ● Telnet HTTP, POP, and SMTP are layered on top of a helper class, lib/net/protocol. Although we don't document the Protocol class here, you should probably study it if you are considering writing your own network client.
class Net::FTP
Parent: Object Version: 1.6
http://www.rubycentral.com/book/lib_network.html (12 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Index: new open Server commands close closed? connect debug_mode debug_mode= dir getbinaryfile gettextfile lastresp list login ls mtime passive passive= putbinaryfile puttextfile resume resume= retrbinary retrlines return_code storbinary storlines welcome
require 'net/ftp' ftp = Net::FTP.new('ftp.netlab.co.jp') ftp.login files = ftp.chdir('pub/lang/ruby/contrib') files = ftp.list('n*') ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024) ftp.close The net/ftp library implements a File Transfer Protocol (FTP) client.
constants FTP_PORT
Default port for FTP connections (21).
class methods new
FTP.new( host=nil, user=nil, passwd=nil, acct=nil) -> aSession Creates and returns a new FTP object. If the host parameter is not nil, a connection is made to that host. Additionally, if the user parameter is not nil, the given user name, password, and (optionally) account are used to log in. See the description of FTP#login on page 488.
open
FTP.open( host, user=nil, passwd=nil, acct=nil) -> aSession A synonym for FTP#new, but with a mandatory host parameter.
instance methods
Server commands
aSession.acct( account ) aSession.chdir( dir ) aSession.delete( remoteFile ) aSession.mdtm( remoteFile ) -> aString aSession.mkdir( dir ) aSession.nlst( dir=nil ) -> anArray aSession.rename( fromname, toname ) aSession.rmdir( dir ) aSession.pwd -> aString aSession.size( remoteFile ) -> anInteger aSession.status -> aString aSession.system -> aString Issues the corresponding server command and returns the result.
close
aSession.close Closes the current connection.
http://www.rubycentral.com/book/lib_network.html (13 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
closed?
aSession.closed? -> true or false
Returns true if the current connection is closed.
connect
aSession.connect( host, port=FTP_PORT ) Establishes an FTP connection to host, optionally overriding the default port. If the environment variable SOCKS_SERVER is set, sets up the connection through a SOCKS proxy. Raises an exception (typically Errno::ECONNREFUSED) if the connection cannot be established.
debug_mode
aSession.debug_mode -> true or false Returns the current debug mode.
debug_mode=
aSession.debug_mode = true or false If the debug mode is true, all traffic to and from the server is written to $stdout.
dir
aSession.dir( [pattern]* ) -> anArray aSession.dir( [pattern]* ) {| line | block } Synonym for FTP#list.
getbinaryfile
aSession.getbinaryfile( remotefile, localfile, blocksize, callback=nil) aSession.getbinaryfile( remotefile, localfile, blocksize ) {| data | block } Retrieves remotefile in binary mode, storing the result in localfile. If callback or an associated block is supplied, calls it, passing in the retrieved data in blocksize chunks.
gettextfile
aSession.gettextfile( remotefile, localfile, callback=nil) aSession.gettextfile( remotefile, localfile ) {| data | block } Retrieves remotefile in ASCII (text) mode, storing the result in localfile. If callback or an associated block is supplied, calls it, passing in the retrieved data one line at a time.
lastresp
aSession.lastresp -> aString Returns the host's last response.
list
aSession.list( [pattern]* ) -> anArray aSession.list( [pattern]* ) {| line | block } Fetches a directory listing of files matching the given pattern(s). If a block is associated with the call, invokes it with each line of the result. Otherwise, returns the result as an array of strings.
login
aSession.login( user="anonymous", passwd=nil, acct=nil ) -> aString
http://www.rubycentral.com/book/lib_network.html (14 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Logs into the remote host. aSession must have been previously connected. If user is the string ``anonymous'' and the password is nil, a password of user@host is synthesized. If the acct parameter is not nil, an FTP ACCT command is sent following the successful login. Raises an exception on error (typically Net::FTPPermError).
ls
aSession.ls( [pattern]* ) -> anArray aSession.ls( [pattern]* ) {| line | block } Synonym for FTP#list.
mtime
aSession.mtime( remoteFile, local=false ) -> aTime Returns the last-modified time of remoteFile, interpreting the server's response as a GMT time if local is false, or as a local time otherwise.
passive
aSession.passive -> true or false Returns the state of the passive flag.
passive=
aSession.passive = true or false Puts the connection into passive mode if true.
putbinaryfile
aSession.putbinaryfile( localfile, remotefile, blocksize, callback=nil) aSession.putbinaryfile( localfile, remotefile, blocksize ) {| data | block } Transfers localfile to the server in binary mode, storing the result in remotefile. If callback or an associated block is supplied, calls it, passing in the transmitted data in blocksize chunks.
puttextfile
aSession.puttextfile( localfile, remotefile, callback=nil) aSession.puttextfile( localfile, remotefile, blocksize ) {| data | block } Transfers localfile to the server in ASCII (text) mode, storing the result in remotefile. If callback or an associated block is supplied, calls it, passing in the transmitted data one line at a time.
resume
aSession.resume -> true or false Returns the status of the resume flag (see FTP#resume=). Default is false.
resume=
aSession.resume=aBoolean Sets the status of the resume flag. When resume is true, partially received files will resume where they left off, instead of starting from the beginning again. This is done by sending a REST command (RESTart incomplete transfer) to the server.
retrbinary
aSession.retrbinary( cmd, blocksize ) {| data | block } Puts the connection into binary (image) mode, issues the given command, and fetches the data returned, passing it to the associated block in chunks of blocksize characters. Note that cmd is a server command (such as ``RETR myfile'').
http://www.rubycentral.com/book/lib_network.html (15 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
retrlines
aSession.retrlines(cmd) {| line | block } Puts the connection into ASCII (text) mode, issues the given command, and passes the resulting data, one line at a time, to the associated block. If no block is given, prints the lines. Note that cmd is a server command (such as ``RETR myfile'').
return_code
aSession.return_code -> aFixnum Returns the return code from the last operation.
storbinary
aSession.storbinary( cmd, fileName, blocksize, callback=nil) aSession.storbinary( cmd, fileName, blocksize ) {| data | block } Puts the connection into binary (image) mode, issues the given server-side command (such as ``STOR myfile''), and sends the contents of the file named fileName to the server. If the optional block is given, or if the callBack parameter is a Proc, also passes it the data, in chunks of blocksize characters.
storlines
aSession.storlines( cmd, fileName, callback=nil) aSession.storlines( cmd, fileName ) {| data | block } Puts the connection into ASCII (text) mode, issues the given server-side command (such as ``STOR myfile''), and sends the contents of the file named fileName to the server, one line at a time. If the optional block is given, or if the callBack parameter is a Proc, also passes it the lines.
welcome
aSession.welcome -> aString Returns the host's welcome message.
class Net::HTTP
Parent: Net::Protocol Version: 1.6
Index: new port start get head post start
require 'net/http' h = Net::HTTP.new('www.pragmaticprogrammer.com', 80) resp, data = h.get('/index.html', nil ) puts "Code = #{resp.code}" puts "Message = #{resp.message}" resp.each {|key, val| printf "%-14s = %-40.40s\n", key, val } p data[0..55] produces:
http://www.rubycentral.com/book/lib_network.html (16 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Code = 200 Message = OK content-type = text/html last-modified = Wed, 21 Feb 2001 18:52:26 GMT date = Mon, 05 Mar 2001 05:26:29 GMT connection = close accept-ranges = bytes etag = "804d98-1a4b-3a940e6a" content-length = 6731 server = Rapidsite/Apa-1.3.4 FrontPage/4.0.4.3 " anArray aSession.get( path, headers=nil) {| result | block } -> anArray Retrieves headers and content from the specified path on the host specified when aSession was created. If specified, the headers parameter is a Hash containing additional header names and values to be sent with the request. The method returns a two-element array. The first element is an HTTPResponse object (documented in the next section). The second element is the page's content. The page's content is also passed to the aHash Retrieves headers from the specified path on the host specified when aSession was created. If specified, the headers parameter is a hash containing additional header names and values to be sent with the request. The method returns a hash of received headers. An exception is raised if an error is encountered. Multiple head calls may be made on aSession.
post
aSession.post( path, data, headers=nil, dest="" ) -> anArray aSession.post( path, data, headers=nil ) {| result | block } -> anArray Sends data to path using an HTTP POST request. headers is a hash containing additional headers. Assigns the result to data or to the block, as for Net_HTTP#get. Returns a two-element array containing an HTTPResponse object and the reply body.
start
aSession.start aSession.start {| aSession | block } Establishes a connection to the host associated with aSession. (start is actually a method in Net::Protocol, but its use is required in HTTP objects.) In the block form, closes the session at the end of the block.
class Net::HTTPResponse Index: [ ] [ ]= code each key? message Represents an HTTP response to a GET or POST request.
http://www.rubycentral.com/book/lib_network.html (18 of 32) [7/20/2001 9:50:10 PM]
Parent: Version: 1.6
Programming Ruby: The Pragmatic Programmer's Guide
instance methods []
aSession[ aKey ] -> aString Returns the header corresponding to the case-insensitive key. For example, a key of ``Content-type'' might return ``text/html''.
[ ]=
aSession[ aKey ] = aString Sets the header corresponding to the case-insensitive key.
code
aSession.code -> aString Returns the result code from the request (for example, ``404'').
each
aSession.each {| key, val | block } Iterates over all the header key-value pairs.
key?
aSession.key?( aKey ) -> true or false Returns true only if a header with the given key exists.
message
aSession.message -> aString Returns the result message from the request (for example, ``Not found'').
class Net::POP
Parent: Net::Protocol Version: 1.6
Index: new each finish mails start
require 'net/pop' pop = Net::POP3.new('server.ruby-stuff.com') pop.start('user', 'secret') do |pop| msg = pop.mails[0] # Print the 'From:' header line puts msg.header.split("\r\n").grep(/^From: /) # Put message to $stdout (by calling aSession Creates and returns a new POP3 object. No connection is made until POP3#start is called.
instance methods each
aSession.each {| popmail | block } Calls the associated block once for each e-mail stored on the server, passing in the corresponding POPMail object.
finish
aSession.finish -> true or false Closes the pop connection. Some servers require that a connection is closed before they honor actions such as deleting mail. Returns false if the connection was never used.
mails
aSession.mails -> anArray Returns an array of POPMail objects, where each object corresponds to an e-mail message stored on the server.
start
aSession.start( user, password ) aSession.start( user, password ) {| pop | block } Establishes a connection to the pop server, using the supplied username and password. Fetches a list of mail held on the server, which may be accessed using the POP3#mails and POP3#each methods. In block form, passes aSession to the block, and closes the connection using finish when the block terminates.
http://www.rubycentral.com/book/lib_network.html (20 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class Net::APOP
Parent: Net::POP3 Version: 1.6
Index: start
instance methods start
aSession.start( user, password ) Establishes a connection to the APOP server.
class Net::POPMail
Parent: Object Version: 1.6
Index: all delete delete! header size top uidl
instance methods all
aSession.all -> aString aSession.all( dest ) aSession.all {| aString | block } Fetches the corresponding e-mail from the server. With no argument or associated block, returns the e-mail as a string. With an argument but no block, appends the e-mail to dest by invoking dest aString Returns the header lines for the corresponding e-mail message.
size
aSession.size -> aFixnum Returns the size in bytes of the corresponding e-mail.
top
aSession.top( lines ) -> aString
http://www.rubycentral.com/book/lib_network.html (21 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns the header lines, plus lines message lines for the corresponding e-mail message.
uidl
aSession.uidl -> aString Returns the server-specific unique identifier for the corresponding e-mail.
class Net::SMTP
Parent: Net::Protocol Version: 1.6
Index: new start ready sendmail start
require 'net/smtp' # --- Send using class methods msg = [ "Subject: Test\n", "\n", "Now is the time\n" ] Net::SMTP.start do |smtp| smtp.sendmail( msg, 'dave@localhost', ['dave'] ) end # --- Send using SMTP object and an adaptor smtp = Net::SMTP.new smtp.start('pragprog.com') smtp.ready('dave@localhost', 'dave') do |a| a.write "Subject: Test1\r\n" a.write "\r\n" a.write "And so is this" end The net/smtp library provides a simple client to send electronic mail using the Simple Mail Transfer Protocol (SMTP).
class methods new
Net::SMTP.new( server='localhost', port=25 ) -> aSession Returns a new SMTP object connected to the given server and port.
start
Net::SMTP.start( server='localhost', port=25, domain=ENV['HOSTNAME'], acct=nil, passwd=nil, authtype=:cram_md5 ) -> aSession Net::SMTP.start( server='localhost', port=25, domain=ENV['HOSTNAME'], acct=nil, passwd=nil, authtype=:cram_md5 ) {| smtp | block } Equivalent to Net::SMTP.new(server, port).start(...). For an explanation of the remainder of the parameters, see the instance method Net_SMTP#start. Creates a new SMTP object. The domain parameter will be used in the initial HELO or EHLO transaction with the SMTP server. In the block form, the smtp object is passed into the block. When the block terminates, the session is closed.
instance methods ready
aSession.ready( from, to ) {| anAdaptor | block }
http://www.rubycentral.com/book/lib_network.html (22 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Equivalent to sendmail(from, to) { ...}. Sends header and body lines to the sendmail server. The from parameter is used as the sender's name in the MAIL FROM: command, and the to is either a string or an array of strings containing the recipients for the RCPT TO: command. The block is passed an adaptor object. Lines are sent to the server by calling the adaptor's write method. The terminating '.' and QUIT are sent automatically.
sendmail
aSession.sendmail( src, from, to ) Sends header and body lines to the sendmail server. The from parameter is used as the sender's name in the MAIL FROM: command, and to is either a string or an array of strings containing the recipients for the RCPT TO: command. Lines to be sent are fetched by invoking src.each. The terminating '.' and QUIT are sent automatically.
start
aSession.start( domain=ENV['HOSTNAME'], acct=nil, passwd=nil, authtype=:cram_md5 ) -> true or false aSession.start( domain=ENV['HOSTNAME'], acct=nil, passwd=nil, authtype=:cram_md5 ) {| smtp | block } -> true or false Starts an SMTP session by connecting to the given domain (host). If acct and passwd are given, authentication will be attempted using the given authentication type (:plain or :cram_md5). If a block is supplied, it will be invoked with aSession as a parameter. The connection will be closed when the block terminates.
class Net::Telnet
Parent: [Socket] Version: 1.6
Index: new binmode binmode= cmd login print telnetmode telnetmode= waitfor write Connect to a localhost, run the ``date'' command, and disconnect. require 'net/telnet' tn = Net::Telnet.new({}) tn.login "guest", "secret" tn.cmd "date" » "date\r\nSun Mar
4 23:26:41 CST 2001\n\r> "
Monitor output as it occurs. We associate a block with each of the library calls; this block is called whenever data becomes available from the host. require 'net/telnet' tn = Net::Telnet.new({}) { |str| print str } tn.login("guest", "secret") { |str| print str } tn.cmd("date") { |str| print str } produces:
http://www.rubycentral.com/book/lib_network.html (23 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Trying localhost... Connected to localhost. Welcome to SuSE Linux 6.2 (i386) - Kernel 2.2.10-smp (pts/12). login: guest Password: Last login: Sun Mar 4 23:26:41 from localhost > date Sun Mar
4 23:26:42 CST 2001
> Get the time from an NTP server. require 'net/telnet' tn = Net::Telnet.new('Host' => 'time.nonexistent.org', 'Port' => 'time', 'Timeout' => 60, 'Telnetmode' => false) atomicTime = tn.recv(4).unpack('N')[0] puts "Atomic time: " + Time.at(atomicTime - 2208988800).to_s puts "Local time: " + Time.now.to_s produces: Atomic time: Sun Mar 04 23:26:34 CST 2001 Local time: Sun Mar 04 23:26:43 CST 2001 The net/telnet library provides a complete implementation of a telnet client and includes features that make it a convenient mechanism for interacting with non-telnet services. Although the class description that follows indicates that Net::Telnet is a subclass of class Socket, this is a lie. In reality, the class delegates to Socket. The net effect is the same: the methods of Socket and its parent, class IO, are available through Net::Telnet objects. The methods new, cmd, login, and waitfor take an optional block. If present, the block is passed output from the server as it is received by the routine. This can be used to provide realtime output, rather than waiting for (for example) a login to complete before displaying the server's response.
class methods new
Net::Telnet.new( options ) -> aSession Net::Telnet.new( options ) {| str | block } -> aSession Connects to a server. options is a Hash with zero or more of the following: Option Binmode Host Port Prompt Telnetmode
Default false localhost 23 /[$%#>]/ true
Timeout
10
Waittime
0
Meaning If true, no end-of-line processing will be performed. Name or address of server's host. Name or number of service to call. Pattern that matches the host's prompt. If false, ignore the majority of telnet embedded escape sequences. Used when talking with a non-telnet server. Time in seconds to wait for a server response (both during connection and during regular data transmission). Time to wait for prompt to appear in received data stream.
http://www.rubycentral.com/book/lib_network.html (24 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
instance methods binmode
aSession.binmode -> true or false Returns the current value of the Binmode flag.
binmode=
aSession.binmode = true or false Sets the Binmode flag, returning the new value.
cmd
aSession.cmd( options ) -> aString aSession.cmd( options ) {| str | block } -> aString Sends a string to the server and waits (using a timeout) for a string that matches a pattern to be returned by the server. If the parameter is not a Hash, it is sent as a string to the server, and the pattern to match and the timeout are the Prompt and Timeout options given when aSession was created. If options is a Hash, then options['String'] is sent to the server. options['Match'] may be used to override the class Prompt parameter, and options['Timeout'] the timeout. The method returns the complete server response.
login
aSession.login( options, password=nil ) -> aString aSession.login( options, password=nil ) {| str | block } -> aString If options is a Hash, a username is taken from options['Name'] and a password from options['Password']; otherwise, options is assumed to be the username, and password the password. The method waits for the server to send the string matching the pattern /login[: ]*\z/ and sends the username. If a password is given, it then waits for the server to send /Password[: ]*\z/ and sends the password. The method returns the full server response.
print
aSession.print( aString ) Sends aString to the server, honoring Telnetmode, Binarymode, and any additional modes negotiated with the server.
telnetmode
aSession.telnetmode -> true or false Returns the current value of the Telnetmode flag.
telnetmode=
aSession.telnetmode= true or false Sets the Telnetmode flag, returning the new value.
waitfor
aSession.waitfor( options ) -> aString aSession.waitfor( options ) {| str | block } -> aString
http://www.rubycentral.com/book/lib_network.html (25 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Waits for the server to respond with a string that matches a string or pattern. If options is not a Hash, it is compared against the cumulative server output as that output is received using options.===. It is likely that you will want to use a regular expression in this case. If options is a Hash, then options['Match'], options['Prompt'], or options['String'] provides the match. In the latter case, the string will be converted to a regular expression before being used. options may also include the keys ``Timeout'' and ``Waittime'' to override the class options of the same names.
write
aSession.write( aString ) Writes aString to the server with no translation.
CGI Development
class CGI
Parent: Object Version: 1.6
Index: escape escapeElement escapeHTML new parse pretty rfc1123_date unescape unescapeElement unescapeHTML [ ] cookies has_key? header keys out params
require "cgi" cgi = CGI.new("html3") # add HTML generation methods cgi.out { CGI.pretty ( cgi.html { cgi.head { cgi.title{"TITLE"} } + cgi.body { cgi.form { cgi.textarea("get_text") + cgi.br + cgi.submit } + cgi.h1 { "This is big!" } + cgi.center { "Jazz Greats of the 20" + cgi.small {"th"} + " century" + cgi.hr } + cgi.p + cgi.table ('BORDER' => '5') { cgi.tr { cgi.td {"Artist"} + cgi.td {"Album"} } + cgi.tr { cgi.td {"Davis, Miles"} + cgi.td {"Kind of Blue"} } } } } ) # CGI.pretty is a method call, not a block } (The output of this script is shown in Figure 26.2 on page 503.) The CGI class provides support for programs used as a Web server CGI (Common Gateway Interface) script. It contains several methods for accessing fields in a CGI form, manipulating ``cookies'' and the environment, and outputting formatted HTML. Since environment variables contain a lot of useful information for a CGI script, CGI makes accessing them very easy---environment variables are accessible as attributes of CGI objects. For instance, cgi.auth_type returns the value of ENV["AUTH_TYPE"]. To create the method name, the environment variable name is translated to all lowercase, and the ``HTTP_'' prefix is stripped off. Thus, HTTP_USER_AGENT would http://www.rubycentral.com/book/lib_network.html (26 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
be available as the method user_agent. Cookies are represented using a separate object of class CGI::Cookie, containing the following accessors: Accessor name value path domain expires
Description Name of this cookie Array of values Path (optional) Domain (optional) Time of expiry, defaults to Time::now (optional)
secure
true for a secure cookie
Figure not available... You create a cookie object using CGI_Cookie#new, which takes as arguments the accessors listed above, or CGI_Cookie#parse, which takes an encoded string and returns a cookie object.
class methods escape
CGI.escape( aString ) -> aNewString Returns a URL-encoded string made from the given argument, where unsafe characters (not alphanumeric, ``_'', ``-'', or ``.'') are encoded using ``%xx'' escapes.
escapeElement
CGI.escapeElement( aString [, elements]* ) -> aNewString Returns a string made from the given argument with certain HTML-special characters escaped. The HTML elements given in elements will be escaped; other HTML elements will not be affected. print CGI::escapeElement('
', "A", "IMG") produces:
escapeHTML
CGI.escapeHTML( aString ) -> aNewString Returns a string made from the given argument with HTML-special characters (such as ``&'',``"'',``'') quoted using ``&'', ``"'', ``'', and so on.
new
CGI.new( [aString]* ) -> aSession Returns a new CGI object. If HTML output is required, the desired standards level must be given in aString (otherwise, no output routines will be created). The level may be one of: String Standards Level String Standards Level ``html3'' HTML 3.2 ``html4'' HTML 4.0 Strict ``html4Tr'' HTML 4.0 Transitional ``html4Fr'' HTML 4.0 Frameset
parse
CGI.parse( aString ) -> aHash Parses a query string and returns a hash of its key-value pairs.
pretty
CGI.pretty( anHTMLString, aLeaderString=" " ) -> aSession
http://www.rubycentral.com/book/lib_network.html (27 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Formats the given anHTMLString in a nice, readable format, optionally prefixing each line with aLeaderString.
rfc1123_date
CGI.rfc1123_date( aTime ) -> aString Returns a string representing the given time according to RFC 1123 (for instance, Mon, 1 Jan 2001 00:00:00 GMT).
unescape
CGI.unescape( aString ) -> aNewString Returns a string containing ``unsafe'' characters made from the given URL-encoded argument, where unsafe characters were encoded using ``%'' escapes.
unescapeElement
CGI.unescapeElement( aString [, elements]* ) -> aNewString Returns a string with the selected escaped HTML elements expanded to the actual characters.
unescapeHTML
CGI.unescapeHTML( aString ) -> aNewString Returns a string made from the given argument with HTML-special quoted characters expanded to the actual characters.
instance methods []
aSession[ [aString]+ ] -> anArray Returns the values of the given field names from the CGI form in an Array. See the note on multipart forms on page 507.
cookies
aSession.cookies -> aHash Returns a new Hash object containing key-value pairs of cookie keys and values.
has_key?
aSession.has_key( aString ) -> true or false Returns true if the form contains a field named aString.
header
aSession.header( aContentType="text/html" ) -> aString aSession.header( aHash ) -> aString Returns a string containing the given headers (in the MOD_RUBY environment, the resulting header is sent immediately instead). If a hash is given as an argument, then the key-value pairs will be used to generate headers.
keys
aSession.keys -> anArray Returns an array of all existing field names for the form.
out
aSession.out( aContentType="text/html" ) { block } -> nil aSession.out( aHash ) { block } -> nil Generates HTML output using the results of the block as the content. Headers are generated as with CGI#header. See the example at the start of this section.
http://www.rubycentral.com/book/lib_network.html (28 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
params
aSession.params -> aHash
Returns a new Hash object containing key-value pairs of field names and values from the form.
HTML Output Methods In addition, CGI supports the following HTML output methods. Each of these methods is named after the corresponding HTML feature (or close to it). Those tags that require content (such as blockquote) take an optional block; the block should return a String that will be used as the content for the feature. These methods may take arguments as indicated, or as a hash with the given names as keys. \ a( url ) a( HREF => ) base( url ) base( HREF => ) blockquote( cite="" ) { aString } blockquote( CITE => ) { aString } caption( align=nil ) { aString } caption( ALIGN => ) { aString } checkbox( name=nil, value=nil, checked=nil ) checkbox( NAME, VALUE, CHECKED => ) checkbox_group( name=nil, [items]+ ) checkbox_group( NAME, VALUES => ) Items may be individual String names, or any of: an array of [ name, checked ], an array of [ value, name ], or an array of [ value, name, checked ]. The value for the hash key VALUES should be an array of these items. file_field( name="", size=20, maxlength=nil ) file_field( NAME, SIZE, MAXLENGTH => ) form( method="post", action=nil, enctype="application/x-www-form-urlencoded" ) { aStr } form( METHOD, ACTION, ENCTYPE => ) { aStr } hidden( name="", value=nil ) hidden( NAME, VALUE => ) html( ) { aString } html( PRETTY, DOCTYPE => ) { aString } img_button( src="", name=nil, alt=nil ) img_button( SRC, NAME, ALT => ) img( src="", alt="", width=nil, height=nil ) img( SRC, ALT, WIDTH, HEIGHT => ) multipart_form( action=nil, enctype="multipart/form-data" ) { aString } multipart_form( METHOD, ACTION, ENCTYPE => ) { aString } password_field( name="", value=nil, size=40, maxlength=nil ) password_field( NAME, VALUE, SIZE, MAXLENGTH => )
http://www.rubycentral.com/book/lib_network.html (29 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
popup_menu( name="", items ) popup_menu( NAME, SIZE, MULTIPLE, VALUES (array of items) => ) Items may be individual String names, or any of: an array of [ name, selected ], an array of [ value, name ], or an array of [ value, name, selected ]. The value for the hash key VALUES should be an array of these items. radio_button( name="", value=nil, checked=nil ) radio_button( NAME, VALUE, CHECKED => ) radio_group( name="", items ) radio_group( NAME, VALUES (array of items) => ) Items may be individual String names, or any of: an array of [ name, selected ], an array of [ value, name ], or an array of [ value, name, selected ]. The value for the hash key VALUES should be an array of these items. reset( value=nil, name=nil ) reset( VALUE, NAME => ) scrolling_list( alias for popup_menu ) scrolling_list( => ) submit( value=nil, name=nil ) submit( VALUE, NAME => ) text_field( name="", value=nil, size=40, maxlength=nil ) text_field( NAME, VALUE, SIZE, MAXLENGTH => ) textarea( name="", cols=70, rows=10 ) textarea( NAME, COLS, ROWS => ) \ In addition, all HTML tags are supported as methods, including title, head, body, br, pre, and so on. The block given to the method must return a String, which will be used as the content for that tag type. Not all tags require content:
, for example, does not. The available tags vary according to the supported HTML level---Table 26.1 on page 507 lists the complete set. For these methods, you can pass in a hash with attributes for the given tag. For instance, you might pass in 'BORDER'=>'5' to the table method to set the border width of the table. HTML tags available as methods {HTML 3} a address applet area b base basefont big blockquote body br caption center cite code dd dfn dir div dl dt em font form h1 h2 h3 h4 h5 h6 head hr html i img input isindex kbd li link listing map menu meta ol option p param plaintext pre samp script select small strike strong style sub sup table td textarea th title tr tt u ul var xmp {HTML 4} a abbr acronym address area b base bdo big blockquote body br button caption cite code col colgroup dd del dfn div dl dt em fieldset form h1 h2 h3 h4 h5 h6 head hr html i img input ins kbd label legend li link map meta noscript object ol optgroup option p param pre q samp script select small span strong style sub sup table tbody td textarea tfoot th thead title tr tt ul var {HTML 4 Transitional} a abbr acronym address applet area b base basefont bdo big blockquote body br button caption center cite code col colgroup dd del dfn dir div dl dt em fieldset font form h1 h2 h3 h4 h5 h6 head hr html i iframe img input ins isindex kbd label legend li link map menu meta noframes noscript object ol optgroup option p param pre q s samp script select small span strike strong style sub sup table tbody td textarea tfoot th thead title tr tt u ul var frame frameset
http://www.rubycentral.com/book/lib_network.html (30 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Multipart Form Values When dealing with a multipart form, the array returned by CGI#[] is composed of objects of class Tempfile, with the following dynamically added methods: Method read local_path original_filename content_type
Description Body Path to local file containing the content Original filename of the content Content type
class CGI::Session
Parent: Object Version: 1.6
Index: new [ ] [ ]= delete update A CGI::Session maintains a persistent state for web users in a CGI environment. Sessions may be memory-resident or may be stored on disk. See the discussion on page 148 for details.
class methods new
CGI::Session.new( aCgi, [aHash]* ) -> aSession Returns a new session object for the CGI query. Options that may be given in aHash include: Option session_key session_id new_session
Description Name of CGI key for session identification. Value of session id. If true, create a new session id for this session. If false, use an existing session identified by session_id. If omitted, use an existing session if available, otherwise create a new one. database_manager Class to use to save sessions; may be CGI::Session::FileStore or CGI::Session::MemoryStore (or user defined if you're brave). Default is FileStore. tmpdir For FileStore, directory for session files. prefix For FileStore, prefix of session filenames.
instance methods []
aSession[ aKey ] -> aValue Returns the value for the given key.
[ ]=
aSession[ aKey ] = aValue -> aValue Sets the value for the given key.
http://www.rubycentral.com/book/lib_network.html (31 of 32) [7/20/2001 9:50:10 PM]
Programming Ruby: The Pragmatic Programmer's Guide
delete
aSession.delete
Calls the delete method of the underlying database manager. For FileStore, deletes the physical file containing the session. For MemoryStore, removes the session from memory.
update
aSession.update Calls the update method of the underlying database manager. For FileStore, writes the session data out to disk. Has no effect with MemoryStore.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Microsoft Windows Support The three libraries documented in this chapter turn Ruby into a powerful and convenient Windows scripting language. Now you have the power to control your applications, but in a controlled, object-oriented environment.
class WIN32OLE
Parent: Object Version: 1.6
Index: connect const_load new [ ] [ ]= each invoke
require 'win32ole' ie = WIN32OLE.new('InternetExplorer.Application') ie.visible = true ie.gohome WIN32OLE provides a client interface to Windows 32 OLE Automation servers. See the tutorial description on page 166 for more information.
constants WIN32OLE::VERSION
Current version number
http://www.rubycentral.com/book/lib_windows.html (1 of 6) [7/20/2001 9:50:12 PM]
Programming Ruby: The Pragmatic Programmer's Guide
class methods connect
WIN32OLE.connect( aString ) -> wapi Returns a new OLE automation client connected to an existing instance of the named automation server.
const_load
WIN32OLE.const_load( wapi, [aClass=WIN32OLE] ) -> nil Defines the constants from the specified automation server as class constants in aClass.
new
WIN32OLE.new( aString ) -> wapi Returns a new OLE automation client connected to a new instance of the automation server named by aString.
instance methods []
wapi[ aString ] -> anObject Returns the named property from the OLE automation object.
[ ]=
wapi[ aString ] = aValue -> nil Sets the named property in the OLE automation object.
each
wapi.each {| anObj | block } -> nil Iterates over each item of this OLE server that supports the IEnumVARIANT interface.
invoke
wapi.invoke ( aCmdString, [args]* ) -> anObject
http://www.rubycentral.com/book/lib_windows.html (2 of 6) [7/20/2001 9:50:12 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Invokes the command given in aCmdString with the given args. args may be a Hash of named parameters and values. You don't need to call invoke explicitly; this class uses method_missing to forward calls through invoke, so you can simply use the OLE methods as methods of this class.
class WIN32OLE_EVENT
Parent: Object Version: 1.6
Index: message_loop new on_event This (slightly modified) example from the Win32OLE 0.1.1 distribution shows the use of an event sink. require 'win32ole' $urls = [] def navigate(url) $urls wapi
http://www.rubycentral.com/book/lib_windows.html (5 of 6) [7/20/2001 9:50:12 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Returns a new object representing a Windows 32 API function. dllname is the name of the DLL containing the function, such as ``user32'' or ``kernel32.'' procname is the name of the desired function. importArray is an array of strings representing the types of arguments to the function. export is a string representing the return type of the function. Strings ``n'' and ``l'' represent numbers, ``i'' represent integers, ``p'' represents pointers to data stored in a string, and ``v'' represents a void type (used for export parameters only). These strings are case-insensitive.
instance methods call
wapi.call( [args]* ) -> anObject Calls this API function with the given arguments, which must match the signature specified to new.
Call
wapi.Call( [args]* ) -> anObject Synonym for Win32API#call.
Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous
Embedded Documentation Figure not available... Figure not available... So you've written a masterpiece, a class in a class of its own, and you'd like to share it with the world. But, being a responsible developer, you feel the need to document your creation. What do you do? The simplest solution is to use Ruby's built-in documentation format, RD, and rdtool, a Ruby utility suite that converts this documentation into a variety of output formats. rdtool scans a file for =begin and =end{=begin...=end@{=begin pairs, and extracts the text between them all. This text is assumed to be documentation in RD format. The text is then processed according to a simple set of rules: ● Lines of text flush to the left margin are converted to paragraphs. ● Lines starting with one to four equals signs are headings. ``='' is a first-level heading, ``=='' a second-level heading, and so on. ``+'' and ``++'' can be used to signal fifth- and sixth-level headings if you really want to go that deep. = Top Level Heading == Second Level Heading ... ●
Lines in which the first nonspace is an asterisk indicate the beginnings of bullet lists. Continuation lines for each bullet item should line up with the text on the first line. Lists may be nested. This is normal text * start of a multiline bullet item * and another * nested item * second nested * third item at top level
http://www.rubycentral.com/book/rdtool.html (1 of 5) [7/20/2001 9:50:13 PM]
Programming Ruby: The Pragmatic Programmer's Guide
●
Lines where the first nonspace characters are digits between parentheses indicate numbered lists. The actual digits used are ignored. Again, lists may be nested. (1) A numbered item * subitem in a bulleted list * subitem (2) Second numbered item (9) This will actually be labeled '3.'
●
Lines starting with a colon indicate labeled lists. The text on the colon line is the label. The immediately following text (which may not be indented less than the label) is the descriptive text. Again, each type of list may be nested. : red when the light is red, you must stop : amber the amber light means that things are about to change. Either: * step on the gas, or * slam on the brakes : green green means GO
Lines starting with three minus signs are a special kind of labeled list, when the labels are method names and signatures. The source in Figure A.1 on page 518 shows a handful of these in action. Indented text that isn't part of a list is set verbatim (such as the stuff under ``Synopsis'' in Figures A.1 and A.2). ●
Inline Formatting Within blocks of text and headings, you can use special inline sequences to control text formatting. All sequences are nested within a set of double parentheses. Sequence ((*emphasis*)) (({code stuff})) ((|variable|)) ((%type me%)) ((:index term:)) (()) ((-footnote-))
Example emphasis code stuff variable type me index term reference
(('verb'))
verb
text.4
Intended Use Emphasis (normally italic) Code Variable name Keyboard input Something to be indexed Hyperlink reference Footnote text. A reference is placed inline, and the text of the footnote appears at the bottom of the page. Verbatim text
http://www.rubycentral.com/book/rdtool.html (2 of 5) [7/20/2001 9:50:13 PM]
Programming Ruby: The Pragmatic Programmer's Guide
Cross References The content of headings, the labels of labeled lists, and the names of methods are automatically made into potential cross reference targets. You make links to these targets from elsewhere in the document by citing their contents in the (()) construct. = Synopsis ... See (()) for details. .. == Instance Methods --- Tempfile.open( filename ) Opens the file... == Return Codes .. The method (()) raises an (({IOException}))... If a reference starts with ``URL:'', rdtool attempts to format it as an external hyperlink. The reference (()) generates a link to label but places the text ``display part'' in the output document. This is used in the description section of the example in Figure A.1 on page 518 to generate references to the method names: perspective, apart from the unusual (()), ... This construct displays the word ``new'' in code font but uses it as a hyperlink to the method Tempfile.new.
Method Names rdtool makes certain assumptions about the format of method names. Class or module methods should appear as Class.method, instance methods as Class#method, and class or module constants as Class::Const.
http://www.rubycentral.com/book/rdtool.html (3 of 5) [7/20/2001 9:50:13 PM]
Programming Ruby: The Pragmatic Programmer's Guide
--- Tempfile::IOWRITE Open the file write-only. ... --- Tempfile.new( filename ) Constructs a temporary file in the given directory. The file ... --- Tempfile#open Reopens ((|aTempfile|)) using mode ``r+'', which allows reading ..
Including Other Files The contents of filename will be inserted wherever the document contains ", :PROMPT_S => "%N(%m):%03n:%i%l ", :PROMPT_C => "%N(%m):%03n:%i* ", :RETURN => "%s\n" }
Restrictions Because of the way irb works, there is a minor incompatibility between it and the standard Ruby interpreter. The problem lies in the determination of local variables. Normally, Ruby looks for an assignment statement to determine if something is a variable---if a name hasn't been assigned to, then Ruby assumes that name is a method call. eval "a = 0" a produces: prog.rb:2: undefined local variable or method `a' for # (NameError) In this case, the assignment is there, but it's within a string, so Ruby doesn't take it into account. irb, on the other hand, executes statements as they are entered. irb(main):001:0> eval "a = 0" 0 irb(main):002:0> a 0 In irb, the assignment was executed before the second line was encountered, so ``a'' is correctly identified as a local variable. If you need to match the Ruby behavior more closely, you can place these statements within a begin/end pair. irb(main):001:0> begin irb(main):002:1* eval "a = 0" irb(main):003:1> a irb(main):004:1> end NameError: undefined local variable or method `a' (irb):3:in `irb_binding'
http://www.rubycentral.com/book/irb.html (6 of 9) [7/20/2001 9:50:14 PM]
Programming Ruby: The Pragmatic Programmer's Guide
rtags, xmp, and the Frame Class The base version of irb is installed along with Ruby itself. But there is an extended version of irb in the archives containing a few extra goodies that need mentioning.
rtags rtags is a command used to create a TAGS file for use with either the emacs or vi editor. rtags [-vi] [files]... By default, rtags makes a TAGS file suitable for emacs (see etags.el). The -vi option makes a TAGS file for use with vi. rtags needs to be installed in the same manner as irb (that is, you need to install irb in the library path and make a link from irb/rtags.rb to bin/rtags).
xmp irb's xmp is an ``example printer''---that is, a pretty-printer that shows the value of each expression as it is run (much like the script we wrote to format the examples in this book). There is also another stand-alone xmp in the archives. xmp can be used as follows: require "irb/xmp" xmp "Doc Severinsen" artist ==>"Doc Severinsen" Or, it can be used as an object instance. Used in this fashion, the object maintains context between invocations:
http://www.rubycentral.com/book/irb.html (7 of 9) [7/20/2001 9:50:14 PM]
Programming Ruby: The Pragmatic Programmer's Guide
require "irb/xmp" x = XMP.new x.puts "Louis Prima" You can explicitly provide a binding with either form; otherwise, xmp uses the caller's environment. xmp code_string, abinding XMP.new(abinding) Note that xmp does not work with multithreading.
The Frame Class The IRB::Frame class represents the interpreter's stack and allows easy access to the Binding environment in effect at different stack levels. Returns a Binding for the nth context from the top. The 0th context is topmost, most recent frame. IRB::Frame.bottom(n = 0) Returns a Binding for the nth context from the bottom. The 0th context is the bottommost, initial frame. Returns the object (the sender) that invoked the current method. IRB::Frame.sender IRB::Frame.top(n = 0)
You can use this facility, for instance, to examine local variables from the method that called the current method:
http://www.rubycentral.com/book/irb.html (8 of 9) [7/20/2001 9:50:14 PM]
Programming Ruby: The Pragmatic Programmer's Guide
require 'irb/frame' def outie b = IRB::Frame.top(1) eval "p my_local", b end def innie my_local = 102.7 outie end innie produces: 102.7 Note that this doesn't work with multithreaded programs. Previous
Programming Ruby: The Pragmatic Programmer's Guide
Programming Ruby The Pragmatic Programmer's Guide Previous