Archive for the ‘Groovy’ Category
![](https://www.lancard.com/blog/wp-content/themes/default/img/icon/uchida.jpg)
Groovyやってみる!
- 2014/09/18
- uchida
1.インストール
brewでいれるよりgvmが流行と書いてあったので以下の手順でインストール
curl -s get.gvmtool.net | bash
これを設定しなさいって書いてあったので設定する
export GROOVY_HOME=/usr/local/opt/groovy/libexec
インストール実行
gvm install groovy
2.開発環境
EclipseにGroovy-Eclipseプラグインを入れます。
http://dist.springsource.org/milestone/GRECLIPSE/e4.4/
3.ファイルを読み込んでコンソールに出力するだけのコード
package com.lancard.yuichi
class FileRead {
static main(args) {
new File(args[0]).eachLine {
println it
}
}
}
これだけで、ファイルを開いてtry catchで囲んでfinallyでcloseされてっていう。すごい・・。
「it」は各行の内容の暗黙の変数らしい。
4.クロージャも使える
package com.lancard.yuichi
import groovy.lang.Closure;
class Add {
static main(args) {
Closure add = add()
add.call()//1
add.call()//2
add.call()//3
add.call()//4
}
static Closure add(){
def x = 0
Closure closure = {
x++
println x
}
closure;
}
}
5.回数指定でクロージャ実行
package com.lancard.yuichi
class Times {
static main(args) {
3.times {
println it
}
//0 1 2
}
}
色々簡単で面白い!
![Share on Facebook Facebook](https://www.lancard.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/48x48/facebook.png)
![Share on Twitter twitter](https://www.lancard.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/48x48/twitter.png)
![Share on Linkedin linkedin](https://www.lancard.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/48x48/linkedin.png)
![Share on tumblr tumblr](https://www.lancard.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/48x48/tumblr.png)
![Share by email mail](https://www.lancard.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/48x48/mail.png)