IronRuby と irpack を使ってみた
WinForms でハローワールド
「.NETでRuby開発を体験してみよう − @IT」を参考に Ruby から WinForms を使ってみた。
リンク先のコードをもとに書いたファイル main.rb がこれ。
# coding: utf-8 require 'mscorlib' require 'System.Windows.Forms' require 'System.Drawing' # .NET の名前空間はモジュールになっているので include できる。 include System::Windows::Forms include System::Drawing class RubyForm < Form def initialize self.Text = "Windows Forms on IronRuby" btn = Button.new btn.Location = Point.new(50, 50) btn.Text = "クリック!" btn.Click do |sender, e| MessageBox.Show("Hello World!") end # ※ 大文字で始まるメソッドは self. を付けないと参照できない。 self.Controls.Add(btn) end end Application.Run(RubyForm.new)
実行してみる。はろーわーるど、はろーわーるど。
>ir main.rb
irpack で .exe にまとめる
irpack は IronRuby のスクリプトを単体で動く .exe にまとめてくれるツール。
>igem install irpack
でインストールできる。ただし、管理者権限のプロンプトから実行しないと多分失敗する。
スクリプトは System.Drawing.dll
と System.Windows.Forms.dll
を参照しているので、これをスクリプトと一緒に固めてもらう必要がある。
今回は、C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0
から該当アセンブリをコピーしてスクリプトと同じフォルダに置いた。
>irpack --window -o click.exe main.rb System.Drawing.dll System.Windows.Forms.dll
これで click.exe ができる。--window
オプションはコマンドプロンプトが開かないようにするために必要。
2014/07/11 03:42 5,015,040 click.exe
やったね!