Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Created October 30, 2009 00:32
Show Gist options
  • Save fumokmm/221986 to your computer and use it in GitHub Desktop.
Save fumokmm/221986 to your computer and use it in GitHub Desktop.
テンプレートを置き換えるデモです。
def template = '○○と●●●は違う'
def list = ['○○', '●●●']
def translator = list.inject([:]){ result, item -> result[item] = getList(item); result }
// こうなる↓
//def translator = [
// '○○' : ['1', '2', '3', '4', '5'],
// '●●●' : ['あああ', 'いいい', 'ううう', 'えええ', 'おおお']
//]
([template] * 5).collect{ tmpl ->
translator.each { tmpl = tmpl.replace( it.key, it.value.sort(){ Math.random() }.pop() ) }; tmpl
}
def getList(str) {
if (str == '○○') return ['1', '2', '3', '4', '5']
if (str == '●●●') return ['あああ', 'いいい', 'ううう', 'えええ', 'おおお']
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment