This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TimeCategory | |
import groovy.time.TimeCategory | |
// 日時計算のための便利なカテゴリ | |
use (TimeCategory) { | |
println 10.hours.ago | |
println 3.days.from.today | |
println 1.year.from.now | |
println((1.minute + 30.seconds).from.now) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab('org.twitter4j:twitter4j:2.2.5') | |
@Grab('org.twitter4j:twitter4j-stream:2.2.5') | |
import twitter4j.* | |
def stream = new TwitterStreamFactory().instance | |
def listener = [ | |
onStatus: { st -> println "$st.user.screenName: $st.text" }, | |
onException: { ex -> ex.printStackTrace() }, | |
] as UserStreamAdapter | |
stream.addListener(listener) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.metaClass.hello = { "Hello, $delegate!" } | |
println "Groovy".hello() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g100pon #73 AntBuilder | |
// AntBuilderによるファイルダウンロードと解凍 | |
def file = 'groovy-binary-1.7.5.zip' | |
def url = "http://dist.groovy.codehaus.org/distributions/$file" | |
new AntBuilder().with { | |
get(src:url, dest:'.') | |
unzip(src:"./$file", dest:'.') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g100pon #25 tail -f | |
def input | |
if (args.size()) { | |
def file = new File(args[0]) | |
input = new FileReader(file) | |
def pos = file.size() - 80 * 10 // 十行分(かなり適当) | |
if (pos > 0) { | |
input.skip(pos) // ラスト10行あたりまでスキップ | |
input.readLine() // 中途半端な行を空読み |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g100pon #80 webサーバ(Jetty) | |
// groovyletとテンプレートにも対応した超簡易webアプリケーションサーバ | |
@Grab('org.eclipse.jetty:jetty-server:7.0+') | |
@Grab('org.eclipse.jetty:jetty-servlet:7.0+') | |
import org.eclipse.jetty.server.* | |
import org.eclipse.jetty.server.handler.* | |
import org.eclipse.jetty.servlet.* | |
import groovy.servlet.* | |
server = new Server(8080) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g100pon #54 duck typing | |
class Dog { | |
void cry() { println "Bowwow!" } | |
} | |
class Cat { | |
void cry() { println "Meow!" } | |
} | |
// DogとCatは共通のスーパークラスもインタフェースも持たないが、以下のように扱える(Javaでは無理) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g100pon #45 日時の演算処理。例えば「現在時刻の3日と4時間後を求める」「特定の時刻間の時間数を求める」 | |
use (groovy.time.TimeCategory) { | |
// 現在時刻の3日と4時間後を求める | |
def now = new Date() | |
def d = now + 3.day + 4.hours | |
println d | |
// 特定の時刻間の時間数を求める | |
def diff = d - now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g100pon #96 jlineで行編集/補完/ヒストリ付きコマンドライン | |
import jline.* | |
reader = new ConsoleReader() | |
user = reader.readLine('User Name: ').trim() | |
passwd = reader.readLine('Password: ', '*' as char) | |
println "User $user logged in." | |
reader.history.clear() | |
commands = ['bye', 'test', 'debug', 'verbose', 'help'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g100pon #14 twitterにつぶやく | |
@Grab('org.twitter4j:twitter4j-core:[2.1,)') | |
import twitter4j.* | |
twitter = new TwitterFactory().instance | |
twitter.updateStatus 'Groovy rocks!' |