Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用数组的随机词的随机短语_Ruby On Rails_Ruby_Arrays_Variables - Fatal编程技术网

Ruby on rails 使用数组的随机词的随机短语

Ruby on rails 使用数组的随机词的随机短语,ruby-on-rails,ruby,arrays,variables,Ruby On Rails,Ruby,Arrays,Variables,我正在尝试制作一个随机短语生成器,它由随机单词组成。 我有一些单词数组和一些句子数组 代码如下: noun = ['noun1','noun2','noun3'...] @noun = noun.shuffle.sample verb = ['verb1','verb2','verb3'] @verb = verb.shuffle.sample ... # here are some more words phrase = [['@noun','@verb'...],['@verb',

我正在尝试制作一个随机短语生成器,它由随机单词组成。 我有一些单词数组和一些句子数组

代码如下:

noun = ['noun1','noun2','noun3'...]
@noun = noun.shuffle.sample

verb = ['verb1','verb2','verb3']
@verb = verb.shuffle.sample 

... # here are some more words

phrase = [['@noun','@verb'...],['@verb', '@noun'...],[...] ...] # here're some phrases
@phrase = phrase.shuffle.sample
下面是动词片段:

输出不是渲染数组元素,只是渲染它们的名称:

["@noun", "@verb", ...]

你用的是单引号。请尝试以下方法:

phrase = [[@noun, @verb...]

您所做的是输出字符串而不是变量。查看以了解有关字符串的更多信息以及如何使用它们。

您必须删除
短语
行中的引号:

phrase = [[@noun,@verb], [@verb, @noun]] 

因为您想要一个包含变量
@noon
@verb
中的值的数组,而不是一个包含单词'@noon'和'@verb'

的字符串的数组,所以在
示例
之前对
进行洗牌是没有意义的。这是多余的。你的问题是什么?非常感谢!现在看来很明显……你愿意接受这个答案吗?:)是的,再次谢谢你。现在一切顺利。