Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ruby / Yukihiro Matsumoto_Programming Ruby.doc
Скачиваний:
121
Добавлен:
06.06.2015
Размер:
2.71 Mб
Скачать

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.

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 putsexpression 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 sampledirectory 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 fromputs(which isnil).

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 haveirb---Interactive Ruby.irbis 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 517. We recommend that you get familiar withirbso 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.]

#!/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.comandhttp://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:rubybook@pragmaticprogrammer.com

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/rubyalso 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 Datemodule. Guy Decoux and Clemens Hintze patiently answered our questions about writing Ruby extensions, and Masaki Suketa helped us understand theWinOLEmodule.

Although much of the original Ruby documentation is in Japanese, there is a growing body of English 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 ThomasandAndy HuntTHEPRAGMATICPROGRAMMERShttp://www.pragmaticprogrammer.com

Соседние файлы в папке Ruby