Try faker 是一个很酷的 gem,可以生成单词、电子邮件、网址或任何你需要的东西
https://github.com/stympy/faker
我用过很多项目。
hb@hora ~ » irb
2.2.3 :001 > require 'faker'
=> true
2.2.3 :002 > Faker::Lorem.sentence(3)
=> "Ea esse ex."
2.2.3 :003 > Faker::Lorem.sentence(3)
=> "Fugiat odio harum."
2.2.3 :004 > Faker::Lorem.words
=> ["consequuntur", "labore", "optio"]
2.2.3 :005 > Faker::Lorem.word
=> "error"
2.2.3 :006 >
但如果您无法添加外部 gem,您可以创建自己的数组/字典
2.2.3 :013 > dict
=> ["Editors", "and", "critics", "of", "the", "plays", "disdaining", "the", "showiness", "and", "melodrama", "of", "Shakespearean", "stage", "representation", "began", "to", "focus", "on", "Shakespeare", "as", "a", "dramatic", "poet", "to", "be", "studied", "on", "the", "printed", "page", "rather", "than", "in", "the", "theatre", "The", "rift", "between", "Shakespeare", "on", "the", "stage", "and", "Shakespeare", "on", "the", "page", "was", "at", "its", "widest", "in", "the", "early", "19th", "century", "at", "a", "time", "when", "both", "forms", "of", "Shakespeare", "were", "hitting", "peaks", "of", "fame", "and", "popularity", "theatrical"]
2.2.3 :014 > dict.sample
=> "the"
2.2.3 :015 > dict.sample
=> "a"
2.2.3 :016 > dict.sample
=> "disdaining"
2.2.3 :017 > dict.sample
=> "century"
2.2.3 :018 >
该词典是从维基百科的文本复制粘贴到我自己的 irb 然后扫描所有 /w+/
2.2.3 :023 > dict='n his own time, William Shakespeare (1564–1616) was rated as merely one among many talented playwrights and poets, but since the late 17th century he has been considered the supreme playwright and poet of the English language.'
=> "n his own time, William Shakespeare (1564–1616) was rated as merely one among many talented playwrights and poets, but since the late 17th century he has been considered the supreme playwright and poet of the English language."
2.2.3 :024 > dict.scan(/\w+/)
=> ["n", "his", "own", "time", "William", "Shakespeare", "1564", "1616", "was", "rated", "as", "merely", "one", "among", "many", "talented", "playwrights", "and", "poets", "but", "since", "the", "late", "17th", "century", "he", "has", "been", "considered", "the", "supreme", "playwright", "and", "poet", "of", "the", "English", "language"]