ruby-on-rails - Ruby Rake 无重复数组选择任务
问题描述
我正在尝试学习 Ruby,并且被困在 rake 任务中。每次rails fortune
运行,应该显示不同的运势?我应该使用什么来确保随机选择永远不会背靠背重复相同的响应?
desc "Print a random fortune"
task :fortune do
fortunes = [
"Do not mistake temptation for opportunity",
"A friend asks only for your time not your money",
"Do not confuse recklessness with confidence",
"Bide your time, for success is near",
"Every day in your life is a special occasion"]
ap(fortunes.sample)
end
我应该使用 .each 吗?
解决方案
推荐阅读
- angular - 如何从可拖动的谷歌地图角度保存新位置
- python - 值错误:类应包括所有可以在 y 中的有效标签 - 尝试调用部分拟合时
- javascript - 在角度 8 中单击时加载更多数据
- laravel - Laravel - 在规则请求中验证今天之前 18 年的出生日期
- ios - Swift - UIView 在更改选项卡控制器选项卡时移动
- django - 在模板中扩展 ManyToManyField
- c# - 如何获取使用 EF Core 从存储过程中插入的记录的 id 值
- javascript - 为什么我的子类(ES5 OOP)没有在按钮单击时运行?
- angular - 角度路由器:错误 RangeError:超出最大调用堆栈大小
- javascript - 如何使旋转加载器显示 2 秒,然后显示主要内容并且旋转加载器消失