Groovyはじめの一歩

インストール

とりあえずインストールする。
あらためて書くほどのことでも無いので、適当にぐぐってどうぞ。

インタプリタを試してみる

Rubyならirb
Scalaならscala
Groovyなら、groovysh。

というわけで、

$ groovysh --terminal=unix
Groovy Shell (1.6-beta-1, JVM: 1.6.0_02)
Type 'help' or '\h' for help.
-------------------------------------------------------------------------------
groovy:000> help
help

For information about Groovy, visit:
    http://groovy.codehaus.org

Available commands:
  help     (\h) Display this help message
  ?        (\?) Alias to: help
  exit     (\x) Exit the shell
  quit     (\q) Alias to: exit
  import   (\i) Import a class into the namespace
  display  (\d) Display the current buffer
  clear    (\c) Clear the buffer
  show     (\S) Show variables, classes or imports
  inspect  (\n) Inspect a variable or the last result with the GUI object browser
  purge    (\p) Purge variables, classes, imports or preferences
  edit     (\e) Edit the current buffer
  load     (\l) Load a file or URL into the buffer
  .        (\.) Alias to: load
  save     (\s) Save the current buffer to a file
  record   (\r) Record the current session to a file
  history  (\H) Display, manage and recall edit-line history
  alias    (\a) Create an alias
  set      (\=) Set (or list) preferences

For help on a specific command type:
    help command

groovy:000>

groovy:000> print("hoge")
print("hoge")
hoge===> null
groovy:000>

とかそういう感じ。

オプションの--terminal=unixは、WindowsXP上のcygwin(xterm?)で実行するとgroovyshが無応答になる件の回避策。

Scalaだったら、-Xnojlineのアレ。

同じくJLineをつかってるので、scalaでダメならgroovyshでもダメな環境なわけです。

--terminal=unix だとエラーメッセージとかが赤くなったり、見やすくて便利なJLineのメリットは残しつつ、無応答にならない。いい感じ。

カラーなんていらねーよという男気あふれる人は、--terminal=noneオプションでどうぞ。

写経の前に

ちょっとバージョンは古いけど、とっかかりとしてはちょうどいいので、まずコレを読むことにした。
Groovy - Java用スクリプト言語

※さすがにgroovyshの仕様とかちょっと古いな・・・。


ここから追記。

GroovyConsoleが便利

ソース部と実行部の2ペインのSwingアプリであるGroovyConsoleが結構便利。

$ groovyConsole

で起動する。上のペインにソースを書いてCtrl+Rをすると下のペインで実行されて、結果がそのまま表示される。

参考URL