我写了一个小脚本来生成一个随机的 URL,它可以工作,但是我希望生成的 URL 有点可信,这意味着我希望它生成真实的单词。截至目前,它生成 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
Run Code Online (Sandbox Code Playgroud)
所以我正在寻找的是一种更简单的方法,用真实的单词代替随机的 7 个字符串(请保持在 7 到 10 个字符之间)而不使用外部 gem
我的操作系统是 Windows 7
免责声明:此答案面向寻求系统中此问题解决方案的开发人员
unix。然而,这并没有解决非 unix 系统的这个问题。
您可以使用ruby'ssystem calls来执行此操作。Unix 系统内置有从文件中获取随机行的命令。
好消息,unix 系统也有完整的英语词典,位于usr/share/dict/words. 所以,在红宝石我会做
`shuf -n 1 /usr/share/dict/words`.chomp
=> "dastardly"
Run Code Online (Sandbox Code Playgroud)
注意:这里我使用了backtick作为system调用。并shuf命令从文件中获取随机行。
所以网址是
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"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1561 次 |
| 最近记录: |