IronRuby 2回目 Hello GUI

『.NETでRuby開発を体験してみよう』を見ながらやってみた。(@ITの記事)
C#/VB開発者向けにも書かれているらしいので、Rubyを触っている時間が長い人向けに少しだけ書き直した。
結果は同じ。

require 'mscorlib'
require 'System.Windows.Forms'
require 'System.Drawing'

class RubyForm < System::Windows::Forms::Form

  def initialize
    self.text = "Windows Form on IronRuby"
    btn = System::Windows::Forms::Button.new
    btn.location = System::Drawing::Point.new(50, 50)
    btn.text = "Click Me!"

    btn.click{|sender, e|
      System::Windows::Forms::MessageBox.show("Hello World!")
    }
    
    controls.add(btn)
  end

end

System::Windows::Forms::Application.run(RubyForm.new) 
  • self.textに日本語を指定するのは記事の通りダメっぽい。
  • initializeがrunすると呼ばれるっぽい。
  • ボタンオブジェクトを作って、イベントを登録していって、ラストにcontrolsにaddするらしい
    • イベントの登録はブロックを渡せばいいだけっぽいので、移動させた。
  • ブロックの中のsenderやeをputs inspectで覗いたら以下だった
System.Windows.Forms.Button, Text: Click Me!
System.Windows.Forms.MouseEventArgs
    • textはウィンドウの帯部分に出るものと思ったけど、実は名前か??
    • eはイベントっぽいね。

とりあえず、System::Windows::Formus::Application.RunにSystem::Windows::Forms::Formクラスを渡せばいいらしい!!

なんとなく、tkに似てるな〜と思った。

眠くなってきたので、コードをまじめに追うのは後日。
この日記の修正もその際に!!

オマケ

インストールディレクトリにあるファイルを開いてみた

<?xml version='1.0' encoding='UTF-8'?>
<configuration>
  <configSections>
    <section name='microsoft.scripting' requirePermission='false' type='Microsoft.Scripting.Hosting.Configuration.Section, Microsoft.Scripting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'/>
  </configSections>

  <microsoft.scripting>
    <languages>
      <language extensions='.py' displayName='IronPython 2.6.1' type='IronPython.Runtime.PythonContext, IronPython, Version=2.6.10920.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' names='IronPython;Python;py'/>
      <language extensions='.rb' displayName='IronRuby' type='IronRuby.Runtime.RubyContext, IronRuby, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' names='IronRuby;Ruby;rb'/>
    </languages>

    <options>
      <set language='Ruby' option='LibraryPaths' value='..\lib\IronRuby;..\lib\ruby\site_ruby\1.8;..\lib\ruby\site_ruby;..\lib\ruby\1.8'/>
    </options>
  </microsoft.scripting>
</configuration>

ここのset languageにあるLibraryPathsのvalueが、標準ライブラリへのディレクトリパスらしい。
というより、むしろIronPythonという文字の方が気になる件