尝试使用faker这个很酷的宝石来生成单词、电子邮件、网址或你需要的任何内容。
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"]