JRubyでDSL作ってみた
2012/04/04 akimatter
自己紹介
秋間武志

@akimatter

Java10年、Ruby6年

Ruby Business Commons運営委員

株式会社グルーヴノーツ
テクニカルプロデューサー/プログラマ
ゲーム開発やってます

 http://www.groovenauts.jp/

 https://twitter.com/Groovenauts

 http://www.facebook.com/Groovenauts.Inc
秋間の視点だとこんな感じ

   イーシー・ワン クラウド事業部




   ノーチラス・テクノロジーズ 福岡




      グルーヴノーツ 福岡
2008/04
イーシー・ワンで2008/07に出版するべく
「JRuby徹底入門」を執筆開始。

 秋間は原案のソースコードを書いただけ




              http://www.amazon.co.jp/JRuby-徹底入門-
              株式会社イーシー・ワン/dp/4881666452
2008/06

2008/05 Sunの粋な計らいで、福岡でのJRuby勉強会に
JRuby開発者のCharles NutterとThomas Eneboが来てく
れることが決定

ちょっと頑張っちゃおうかなと決意

SwingをRubyっぽく書けたら面白そう

Rubeusと命名される
Rubeus
Rubeusの狙ったところ

Rubeus provides you an easy access to Java objects
from Ruby scripts on JRuby

JavaをRuby色に染めちゃえ

JRubyでも十分Rubyだけど、もっとRubyっぽくするため

のDSLを提供しよう
in Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JavaSwingExample01 {

        public static void main(String[] args) {
            JFrame frame = new JFrame("Rubeus Swing Example 01");
            frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
            JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
            frame.add(splitPane);
            JPanel panel = new JPanel();
            splitPane.setTopComponent(panel);
            panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
            final JTextField textField = new JTextField();
            panel.add(textField);
            final JTextPane textPane = new JTextPane();
            textField.addKeyListener(new KeyAdapter() {
                    public void keyPressed(KeyEvent event) {
                        if (event.getKeyCode() == 10) {
                            textPane.setText(textPane.getText() + textField.getText() + "n");
                            textField.setText("");
                        }
                    }
                });
            JButton button = new JButton("append");
            panel.add(button);
            button.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent event) {
                        textPane.setText(textPane.getText() + textField.getText() + "n");
                        textField.setText("");
                    }
                });
            JScrollPane scrollPane = new JScrollPane(textPane);
            splitPane.setBottomComponent(scrollPane);
            scrollPane.setPreferredSize(new Dimension(400, 250));
            frame.setSize(400, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
}
require 'rubygems'
require "rubeus"          in JRuby with Rubeus
Rubeus::Swing.irb

JFrame.new("Rubeus Swing Example 01") do |frame|
  frame.layout = BoxLayout.new(:Y_AXIS)
  JSplitPane.new(JSplitPane::VERTICAL_SPLIT) do
    JPanel.new do |panel|
      panel.layout = BoxLayout.new(:X_AXIS)
      @text_field = JTextField.new do |event|
        if event.key_code == 10 # RETURN
          @textpane.text += @text_field.text + "n"
          @text_field.text = ''
        end
      end
      JButton.new("append") do
        @textpane.text += @text_field.text
        @text_field.text = ''
      end
    end
    JScrollPane.new(:preferred_size => [400, 250]) do |pane|
      @textpane = JTextPane.new
    end
  end
  frame.visible = true
end
思い切ったDSLの設計
JButtonのイベントハンドラなんて、クリックされ
た場合しかほとんど使わないんだからコンストラ
クタにブロックで指定すればいいじゃん

JButton.new("append") do
  @textpane.text += @text_field.text
  @text_field.text = ''
end
JDBC with Rubeus
require 'rubygems'
require 'rubeus'
Rubeus::Jdbc.irb
DriverManager.connect("jdbc:derby:test_db") do |conn|
   conn.query("SELECT * FROM TEST ORDER BY ID") do |rs|
     rs.each do |row|
         puts row.to_a.join(",")
     end
   end
end
easy install

Java をインストール

rvm をインストール

rvm install jruby

gem install rubeus
getting started with irb
$ irb
> require ‘rubygems’
> require ‘rubeus’
> Rubeus::Swing.irb
> f = JFrame.new
> f.visible = true
> f.title = “foo”
Charlesに見せた時の反応

JRubyの勉強会で見せたんですが、

“Oh, looks great.” の一言。

だよねー、 使いどころあんまり無いし

英語も通じてない? (́・ω・`)
なので


もうすっかり忘れていたわけですよ。



しかし2年後
Thomasからのメール

  Howdy,

Can someone look at and apply files/patches submitted in Issues 13 and 14?

  Also I am demo'ing Rubeus in a book and these
fixes also need to be put into a new Rubeus release.
Can you also release a new version of Rubeus with
these changes?

-Tom

                     https://groups.google.com/forum/?hl=ja&fromgroups#!topic/rubeus/Y7CsIojnZaQ
うわーお、でもマジ?

本業が忙しすぎて全然メンテしきらんとですよ > <



なのでちょっと振ってみた
Thomasへの返事
   Thank you, Tom.
Are you writing a book !? It's great! I'm looking forward to read your book.
I applied your patch and released rubeus-0.0.9 just now. So it will be published
in a few hours.

And I created a repository for rubeus in github
http://github.com/akm/rubeus

If you wish, I append your account as a
collaborator.
Takeshi


                        https://groups.google.com/forum/?hl=ja&fromgroups#!topic/rubeus/Y7CsIojnZaQ
Thomasをコミッターにしちゃえ




申し訳ないけど、それが一番手っ取り早いw
Thomasからの返事
    Thanks for doing this. Yes, several JRuby people are writing a JRuby book:
http://pragprog.com/titles/jruby/using-jruby

One section in our GUI chapter will be covering Rubeus. We are very happy to
promote Rubeus in our book.


Thanks for adding me to your repository on
github. I will help provide fixes if I find any other
problems.


-Tom
                       https://groups.google.com/forum/?hl=ja&fromgroups#!topic/rubeus/Y7CsIojnZaQ
本当にコミットしてくれてるし
本当に載っちゃうし

   http://pragprog.com/book/
   jruby/using-jruby
まとめ

Rubeusは思い切った割り切りが良かったっぽい

rubyにまつわるツールが便利

 rubygemsで簡単にインストール

 irbで簡単に試せる
http://rubybizcommons.jp/ja/articles/2010/07/28/2010727-rubydays-2010/

LT at JavaOne2012 JVM language BoF #jt12_b101