Form1 に Apollo1, Edit1, Edit2, Memo1, Button1 を置いて,Button1 の OnClick イベントハンドラに以下のように記述する。
procedure TForm1.Button1Click(Sender: TObject);
begin
Apollo1.Script.LoadFromFile('func.rb');
with Apollo1.Params do
begin
Clear;
Add(Edit1.Text);
Add(Edit2.Text);
end;
Apollo1.Execute(Memo1.Lines);
end;
Button1
をクリックすると,Edit1.Text, Edit2.Text
を引数に func.rb
を呼び出す。結果,出力が Memo1.Lines
に入る。
例えば次のような func.rb
を Project1.exe と同じフォルダに入れておく。
- func.rb
-
a, b = ARGV
print a.to_i + b.to_i, "\n"
print a.to_i * b.to_i, "\n"
そして,Edit1, Edit2
を次のように設定する。
Edit1.Text = '2'
Edit2.Text = '3'
この状態で Button1
をクリックすると,Memo1.Lines
は
5
6
となる。Ruby script をまるで関数のように利用できるわけだ。Delphi との組み合わせでは,特に文字列処理やファイル操作に威力を発揮するだろう。