dict文件位于:/usr/share/dict/ word,我需要访问该列表中的单词,并随机为用户放置一个包含4-9个字母的单词。
发布于 2014-08-19 19:57:34
假设单词文件每行包含一个单词:
puts File.read('/usr/share/dict/words').lines.select {|l| (4..9).cover?(l.strip.size)}.sample.strip
发布于 2014-08-19 19:38:55
pick = words.select { |w| w.size > 3 && w.size < 10 }.sample
发布于 2014-08-19 19:28:08
这应该是为了你:
file_contents = File.read("/usr/share/dict/words")
words = file_contents.split("\n")
puts words[rand(0..words.size-1)]
https://stackoverflow.com/questions/25391380
复制相似问题