Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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 带有rand()的数组在本地工作,但在heroku上不工作_Ruby_Heroku_Sinatra - Fatal编程技术网

Ruby 带有rand()的数组在本地工作,但在heroku上不工作

Ruby 带有rand()的数组在本地工作,但在heroku上不工作,ruby,heroku,sinatra,Ruby,Heroku,Sinatra,以下工程在本地进行: require 'sinatra' get '/rand' do ["one", "two", "three"][rand(0..2)] end 然而,在heroku上,我得到一个内部服务器错误 有趣的是,这是有效的: get '/rand' do ["one", "two", "three"][rand(2) - 1] end 为什么?确保您使用的是与本地开发人员机器相同版本的ruby on heroku: 在本地和heroku上运行ruby-v:herok

以下工程在本地进行:

require 'sinatra'

get '/rand' do
  ["one", "two", "three"][rand(0..2)]
end
然而,在heroku上,我得到一个
内部服务器错误

有趣的是,这是有效的:

get '/rand' do
  ["one", "two", "three"][rand(2) - 1]
end

为什么?

确保您使用的是与本地开发人员机器相同版本的ruby on heroku:

在本地和heroku上运行
ruby-v
heroku运行“ruby-v”


然后,通过在GEM文件中添加
ruby“1.9.3”
来设置ruby heroku使用的版本。

原因是rand只在(我相信)1.9.3之后才开始接受范围值。Heroku使用Ruby 1.9.1。您可以通过查看heroku日志来验证这一点。您只需设计一种使用整数值而不是范围值的方法。

当您遇到内部服务器错误时,heroku日志中有什么内容?让我们试试方法