我正在阅读《Ruby on Rails 教程》并看到以下一行代码: railstutorial。
然而,显然
('a'..'z').to_a.shuffle[0..7].join
它创建随机的7个字符域名,如下所示:
hwpcbmze.heroku.com
seyjhflo.heroku.com
jhyicevg.heroku.com
我尝试将这个一行代码转换为Groovy,但是我只能想出以下代码:
def range = ('a'..'z')
def tempList = new ArrayList (range)
Collections.shuffle(tempList)
println tempList[0..7].join()+".heroku.com"
上述代码能否改进并缩短为一行?我试图通过以下方式缩短代码:
println Collections.shuffle(new ArrayList ( ('a'..'z') ))[0..7].join()+".heroku.com"
然而,显然
Collections.shuffle(new ArrayList ( ('a'..'z') ))
返回了一个null
。