首页 > 解决方案 > 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 吗?

标签: ruby-on-railsarraysrake

解决方案


推荐阅读