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
| import groovy.json.JsonBuilder | |
| import groovy.json.JsonOutput | |
| //JsonOutputのサンプル | |
| //サンプルデータ | |
| def data = { | |
| people { | |
| person { | |
| firstName 'Yasuharu' |
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
| import groovy.beans.Bindable | |
| import groovy.json.JsonSlurper | |
| import groovy.swing.SwingBuilder | |
| import java.awt.Font | |
| import javax.swing.DefaultComboBoxModel | |
| import javax.swing.JFrame | |
| import javax.swing.JTextField |
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
| def takeNbytes(str, n) { | |
| buf = '' | |
| for (s in str) { | |
| if ( buf.bytes.size() + s.bytes.size() > n) break | |
| buf += s | |
| } | |
| buf | |
| } | |
| println takeNbytes("あいうえお", 2) |
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
| //GetScoreRuns.groovy - bjリーグの2010-2011チーム別総得点・総失点を出力する | |
| @Grab('net.sourceforge.htmlunit:htmlunit:2.8') | |
| import java.net.URL | |
| import com.gargoylesoftware.htmlunit.BrowserVersion | |
| import com.gargoylesoftware.htmlunit.WebClient | |
| proxyHost = null // set the proxy host name if you are in Firewall | |
| proxyPort = 18080 | |
| if (proxyHost != null) { | |
| c = new WebClient(BrowserVersion.FIREFOX_3, proxyHost , proxyPort) |
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
| import groovy.xml.MarkupBuilder | |
| class HtmlBuilder{ | |
| BuilderSupport builder | |
| HtmlBuilder(BuilderSupport builder = new MarkupBuilder()){ | |
| this.builder = builder | |
| } |
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 #53 いろいろな型変換(toメソッド、キャスト、as) | |
| // toメソッド | |
| // 文字から数値へ | |
| def a = '123'.toInteger() | |
| assert a.class == Integer | |
| def b = '123.45'.toBigDecimal() | |
| assert b.class == BigDecimal | |
| // 数値から文字へ |
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 #66 Expando使用例 | |
| //テスト用メソッド | |
| def testMethod(person) { | |
| //引数personにはプロパティfirstName,familyNameとname()メソッドがあることを前提としている | |
| assert person.name() == "${person.firstName} ${person.familyName}" | |
| } | |
| //Expandoを作成 | |
| def person = new Expando() | |
| //プロパティを追加 |
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 #58 StringクラスのGDKメソッド | |
| // 1.7.3で追加されたメソッド | |
| // capitalize:先頭文字を大文字化 | |
| assert 'foo'.capitalize() == 'Foo' | |
| // expand:タブを空白に展開 | |
| assert '¥tabc'.expand() == ' abc' //引数無しの場合タブは8 | |
| assert '¥tdef¥tghi'.expand(4) == ' def ghi' | |
| // stripIndent:各行の先頭にある共通な空白を除去 | |
| assert ' abc abc¥n def¥n ¥n ghi'.stripIndent() == 'abc abc¥n def¥n¥n ghi' | |
| // stripMargin:各行の先頭に当たる文字が現れるまで空白・コントロール文字を除去 |
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 #70 存在しないメソッド/プロパティアクセスをフック | |
| class Some { | |
| def doMethod() { | |
| println "Method!" | |
| } | |
| String name | |
| def methodMissing(String name, args) { | |
| println "method $name ( $args ) is missing!" | |
| } | |
| def propertyMissing(String name) { |
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 #65 何でも使えるぜGroovyなswitch-case | |
| def check(value) { | |
| switch (value) { | |
| case 0: println 'zero';break // 普通 | |
| case 1..9: println '1to9';break // 範囲指定 | |
| case [10, 12, 14]: println '10or12or14';break // リストで指定 | |
| case {it instanceof Integer && it % 7 == 0}: println '7,14,21,...';break // クロージャ | |
| case ~/gr.*/: println 'gr*';break // 正規表現 | |
| case 'foo': println 'FOO';break // 文字列 | |
| case String: println 'String';break // クラス |