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

Installing eruby in Apache

You can set up an Apache Web server to automatically parse Ruby-embedded documents using eRuby, much in the same way that PHP does. You create Ruby-embedded files with an ``.rhtml'' suffix and configure the Web server to run theerubyexecutable on these documents to produce the desired HTML output.

In order to use erubywith the Apache Web server, you need to perform the following steps.

  • Copy the erubybinary to thecgi-bindirectory.

  • Add the following two lines to httpd.conf:

    AddType application/x-httpd-eruby .rhtml

    Action application/x-httpd-eruby /cgi-bin/eruby

  • If desired, you can also add or replace the DirectoryIndexdirective such that it includesindex.rhtml. This lets you use Ruby to create directory listings for directories that do not contain anindex.html. For instance, the following directive would cause the embedded Ruby scriptindex.rhtmlto be searched for and served if neitherindex.htmlnorindex.shtmlexisted in a directory.

DirectoryIndex index.html index.shtml index.rhtml

Of course, you could also simply use a site-wide Ruby script as well.

DirectoryIndex index.html index.shtml /cgi-bin/index.rb

And that's it! You can now write HTML documents that contain embedded Ruby to generate forms and content dynamically. Be sure to see also the Ruby CGIlibrary, which is documented beginning on page 497.

Improving Performance

You can use Ruby to write CGI programs for the Web, but, as with most CGI programs, the default configuration has to start up a new copy of Ruby with every cgi-bin page access. That's expensive in terms of machine utilization and can be painfully slow for Web surfers. The Apache Web server solves this problem by allowing loadable modules.

Typically, these modules are dynamically loaded and become part of the running Web server process---there is no need to spawn another interpreter over and over again to service requests; the Web server isthe interpreter.

And so we come to mod_ruby(available from the archives), an Apache module that links a full Ruby interpreter into the Apache Web server itself. TheREADMEfile included withmod_rubyprovides details on how to compile and install it.

Once installed and configured, you can run Ruby scripts just like you could without mod_ruby, except that now they will come up much faster.

Ruby Tk

The Ruby Application Archive contains several extensions that provide Ruby with a graphical user interface (GUI), including extensions for Tcl/Tk, GTK, OpenGL, and others.

The Tk extension is bundled in the main distribution and works on both Unix and Windows systems. To use it, you need to have Tk installed on your system. Tk is a large system, and entire books have been written about it, so we won't waste time or resources by delving too deeply into Tk itself, but instead concentrate on how to access Tk features from Ruby. You'll need one of these reference books in order to use Tk with Ruby effectively. The binding we use is closest to the Perl binding, so you probably want to get a copy of Learning Perl/TkorPerl/Tk Pocket Reference.

Tk works along a composition model---that is, you start off by creating a container widget (such as a TkFrameorTkRoot) and then create the widgets that populate it, such as buttons or labels. When you are ready to start the GUI, you invokeTk.mainloop. The Tk engine then takes control of the program, displaying widgets and calling your code in response to GUI events.

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