我已经编写了一个会生成随机网址的小脚本,但是我觉得生成的网址有点可信,这意味着我希望它能生成真正的单词。截至目前,它生成7个随机字符和数字。
def generate_url(length=7)
protocall = %w(https:// http://)
body = rand(36**length).to_s(36)
num = rand(1..999).to_s
url = "#{protocall.sample.to_s}#{body}.com/php?id=#{num}"
puts url
end
#<= http://s857yi5.com/php?id=168
#<= https://6rm0oq3.com/php?id=106
#<= http://skhvk1n.com/php?id=306
所以我正在寻找的是一种更简单的方法,用真正的单词代替随机7字符串(请保持7到10个字符之间)而不使用外部宝石
我的操作系统是Windows 7
答案 0 :(得分:4)
尝试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 >
创建了一个字典,从wikipedia的文本复制粘贴到我自己的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"]
答案 1 :(得分:3)
免责声明:此答案针对在
unix
系统中寻求此问题解决方案的开发人员。但事实并非如此 解决非unix系统的这个问题。
您可以使用ruby
&#39; system calls
来执行此操作。内置的Unix系统具有从文件中获取随机行的命令。
好消息是,unix系统还有usr/share/dict/words
的整个英文字典。所以,在红宝石中我会做的
`shuf -n 1 /usr/share/dict/words`.chomp
=> "dastardly"
注意:我在这里使用backtick
作为system
电话。和shuf
command get you random line from a file。
所以URL就是
random_word = `shuf -n 1 /usr/share/dict/words`.chomp
url = "#{random_word}#{body}.com/php?id=#{num}"
=> "wrongfullythisis_body_part.com/php?id=123"
答案 2 :(得分:2)
你可以通过交替的元音和辅音生成可发音但毫无意义的单词:tifa zakohu ayanipico wis kicevepys ijoxar uhiq ilay og luh tanise rijux tejod kuyasoq zov wu
答案 3 :(得分:1)
您可以在unix系统中使用字典中的随机数字。您通常可以在路径/ usr / share / dict / words
中找到它答案 4 :(得分:0)
我找到了另一种使用字典列表的方法,如果您使用Windows并且可以访问Outlook,则可以使用Outlook default.DIC
文件作为单词列表,这将为您提供大量的单词,全部你要做的就是将它复制到程序中。参考是here