|
|||
Previous < |
Contents ^
|
Next >
|
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 scripts and they will simply run without popping up a DOS
shell.
Win32API
extension.
The Win32API
module is documented beginning on page 508,
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 508).
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 |
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 505 describes the class in detail, but
we'll go over a few of its features here.
Rotation
property
in an Excel chart, you might write
excel = WIN32OLE.new("excel.application") excelchart = excel.Charts.Add() ... excelchart['Rotation'] = 45 puts excelchart['Rotation'] |
WIN32OLE
object. This means that you can set a parameter by
assigning to an object attribute.
excelchart.rotation = 45 r = excelchart.rotation |
rotation
instead of Rotation
.
Song(artist, title, length): rem Visual Basic |
Song title := 'Get It On': rem Visual Basic |
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' ) |
WIN32OLE
object has an
each
method (which takes a block) to accomplish the same
thing.
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.
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(); |
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
workbook.Worksheets(1).Range("A1").value = 1 workbook.Worksheets(1).Range("A2").value = 2 workbook.Worksheets(1).Range("A3").value = 4 workbook.Worksheets(1).Range("A4").value = 8 |
worksheet = workbook.Worksheets(1) worksheet.Range("A1").value = 1 worksheet.Range("A2").value = 2 worksheet.Range("A3").value = 4 worksheet.Range("A4").value = 8 |
Previous < |
Contents ^
|
Next >
|