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

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 TkWidgetbeing used, not your class instance.

Object Creation

Perl/Tk:  $widget = $parent->Widget( [ option => value ] )

Ruby:     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:  -background => color

Ruby:     'background' => color

          { background color }

Remember that the code block scope is different.

Variable References

Perl/Tk:  -textvariable => \$variable

          -textvariable => varRef

Ruby:     ref = TkVariable.new

          'textvariable' => ref

          { textvariable ref }

Use TkVariableto attach a Ruby variable to a widget's value. You can then use thevalueaccessors inTkVariable(TkVariable#valueandTkVariable#value=) to affect the contents of the widget directly.

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.exeandrubyw.exe.

ruby.exeis 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 runruby.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 asruby.exeexcept 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 userubyw.exe. By doing this, you can double-click on Ruby 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 Win32APIextension.

The Win32APImodule is documented beginning on page 508, but here's a quick peek at how it works.

You create a Win32APIobject 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. Win32APIhandles this by using RubyStringobjects to pass the binary data back and forth. You will need to pack and unpack these strings as necessary (see the example on page 508).

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