はじめてのGroovy

  • 749 views
Uploaded on

日本 Grails/Groovy ユーザーグループ名古屋支部 第2回勉強会

日本 Grails/Groovy ユーザーグループ名古屋支部 第2回勉強会

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
749
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
5
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. T.Yamamoto 2009/07/24 @Nagoya def speaker = new Cast(name:”T.Yamamoto”,version:”GN-2009-07-24”)
  • 2. ( ) Web 90%Grails JGGUG( ) Grails 9-11 Grails Acegi Plugin http://d.hatena.ne.jp/mottsnite/
  • 3. def name='World' def list = ["Hello", " ", "World"] println "Hello $name!" list.each{ print it } class Greet { def name Greet(who) { name = who[0].toUpperCase() +who[1..-1] } def salute() { println "Hello $name!" } } def g = new Greet('world') // create object g.salute()
  • 4. import java.util.Date; } return this.black; public Integer getColorId() { } public class Color { return this.colorId; public void setBlack(String black) { private Integer colorId; } this.black = black; private String colorName; public void setColorId(Integer colorId) { } private String cyan; this.colorId = colorId; public String getRgb() { private String magenta; } return this.rgb; private String yellow; public String getColorName() { } private String black; return this.colorName; public void setRgb(String rgb) { private String rgb; } this.rgb = rgb; private String sysIpaddress; public void setColorName(String } private Date sysLastmodified; colorName) { public String getSysIpaddress() { private Integer sysUid; this.colorName = colorName; return this.sysIpaddress; public Color(Integer colorId, String } } colorName, String cyan, public String getCyan() { public void setSysIpaddress(String String magenta, String yellow, String return this.cyan; sysIpaddress) { black, String rgb, } this.sysIpaddress = sysIpaddress; String sysIpaddress, Date public void setCyan(String cyan) { } sysLastmodified, Integer sysUid) { this.cyan = cyan; public Date getSysLastmodified() { this.colorId = colorId; } return this.sysLastmodified; this.colorName = colorName; public String getMagenta() { } this.cyan = cyan; return this.magenta; public void setSysLastmodified(Date this.magenta = magenta; } sysLastmodified) { this.yellow = yellow; public void setMagenta(String magenta) { this.sysLastmodified = sysLastmodified; this.black = black; this.magenta = magenta; } this.rgb = rgb; } public Integer getSysUid() { this.sysIpaddress = sysIpaddress; public String getYellow() { return this.sysUid; this.sysLastmodified = sysLastmodified; return this.yellow; } this.sysUid = sysUid; } public void setSysUid(Integer sysUid) { } public void setYellow(String yellow) { this.sysUid = sysUid; public Color() {} this.yellow = yellow; } public Color(Integer colorId) { } } this.colorId = colorId; public String getBlack() {
  • 5. class Color { Integer colorId String colorName String cyan,magenta,yellow,black,rgb String sysIpaddress Date sysLastmodified Integer sysUid } class Color { Integer colorId, sysUid String colorName,cyan,magenta,yellow,black,rgb,sysIpaddress Date sysLastmodified }
  • 6. String name = 'World' ↓ def name='World' class Dog { def say(){ // ← “Garuuuuu” } }
  • 7. GString """ ${str.size()} $str """
  • 8. // def someMethod(num,clo){ clo(num) } { println it } 0 1 { -> println 300 } 0 // { a -> println a } 1 def clo = { println " "+it } { a=2 -> println a } def clo2 = {one,two=" "-> { a,b -> println a+b } println " ${one} ${two}" { int a -> println a } } clo 1 // clo2 2,1 // // someMethod(1,clo) someMethod(2,clo2)
  • 9. def pattern = "d{3}-d{4}" // def text = "633-0412" // println text ==~ pattern // "true" def m = text =~ pattern println m.matches() // "true" p = ~/d{3}-d{4}/ m = p.matcher(text) println m.matches() // "true"
  • 10. [1,2,3] + [1] == [1,2,3,1] [1,2,3] << 1 == [1,2,3,1] def list = [0,1,2] [1,2,3,1] - [1] == [2,3] list[-1] == 2 assert list[-1..0] == list.reverse() [1,[2,3]].flatten() == [1,2,3] assert list == [list.head()] + list.tail() [1,2,3].reverse() == [3,2,1] // [1,2,3].collect{ it+3 } == [4,5,6] [1,2,3,1].unique() == [1,2,3] list.each{println it} [4,2,1,3].findAll{it%2 == 0} == [4,2] [1,2,3,4].sum() == 10 [4,2,1,3].sort() == [1,2,3,4] // ....
  • 11. def map = [:]// map.each { entry -> println entry.key map.name=" " println entry.value map['age']=20 } map.each { k, v -> println map['name'] println "$k $v" println map.age } for (entry in map) { println "$entry.key $entry.value" }
  • 12. assert (0..10).contains(5) assert (0.0..10.0).containsWithinBounds(3.5) for (item in 0..10) { println item } for (item in 10..0) { println item } (0..<10).each { println it }
  • 13. def file = new File('data.csv') // // file.eachLine { line -> file.withWriter('UTF-8'){ writer -> println line writer << ''' , } ,90 ,78 // ,75 file.splitEachLine(',') {list -> ,65 println list[0] ''' } } // println file.text
  • 14. <html> <head> import groovy.xml.* <title>Page title</title> def page = new MarkupBuilder() </head> page.html{ <body> head {title 'Page title'} <div> body { <li>No.1</li> div { <li>No.2</li> (1..5).each{n-> <li>No.3</li> li "No.$n" <li>No.4</li> } <li>No.5</li> } </div> } </body> } </html>
  • 15. class IntCodec { static String encode(Integer self){self.toString()} static Integer decode(String self){self.toInteger()} } use(IntCodec){ 244.encode() "244".decode() } Integer.metaClass.encode << {delegate.toString()} String.metaClass.decode << {delegate.toInteger()} 244.encode().decode()
  • 16. // Integer.metaClass.methodMissing << { String name, Object args -> Math."$name"(delegate) } println 3.sin() println 3.cos()
  • 17. Gaelyk
  • 18. Grails/Groovy http://www.jggug.org/