如何让 Ruby 的正则表达式在没有内部服务器错误的情况下在 Heroku 中工作?

How can I get Ruby's Regexp to work in Heroku without Internal Server Error?

我有这个程序,我成功地上传到 heroku 没有错误。但是当我在浏览器中打开它时,如果我在其中保留一行使用 Regexp 的代码,我会得到 "internal server error"。如果我把它注释掉就好了。

我看过关于 Heroku 的类似标题的问题 "internal server error",但它们不涉及正则表达式,而且它们没有给我任何关于我需要做什么才能使其工作的想法。例如 the person was using python and a package he had installed was incompatible, that doesn't seem relevant to my issue. This one Heroku + Django Internal server Error 一个人必须导入一个叫做 djcelery 的东西。好吧,也许我必须在某个地方导入一些东西,但我不知道是什么,但它不会是 djcelery,因为我没有使用 django。

C:\rubytest\regexinheroku1>dir /b
aaa.rb
config.ru
Gemfile
Gemfile.lock

C:\rubytest\regexinheroku1>

这里是aaa.rb

C:\rubytest\regexinheroku1>type aaa.rb
require 'sinatra'

get '/' do
z=Regexp.new('.').match?('qwerty')   
"aaaa"
end

这就成功了

C:\rubytest\regexinheroku1>git push heroku master

.... deployed to Heroku

不过问题来了

如果我转到 URL,它会显示 "Internal Server Error"

但是,如果我通过注释掉正则表达式行

来更改aaa.rb
C:\rubytest\regexinheroku1>type aaa.rb
require 'sinatra'

get '/' do
# z=Regexp.new('.').match?('qwerty')   
"aaaa"
end

然后它工作正常,出现一个页面并显示 "aaaa" 正如预期的那样。

如果你想重现这个并且以上还不够,这里是所有步骤

So you see the files

C:\rubytest\regexinheroku2>dir /b
aaa.rb
config.ru
Gemfile

The contents of each file 

C:\rubytest\regexinheroku2>type aaa.rb
require 'sinatra'

get '/' do
z=Regexp.new('.').match?('qwerty')  # if I uncomment this, I get internal server error
"aaaa"
end


C:\rubytest\regexinheroku2>type config.ru
require './aaa'
run Sinatra::Application
C:\rubytest\regexinheroku2>

C:\rubytest\regexinheroku2>type Gemfile
source "http://rubygems.org/"
gem 'sinatra'
gem "haml"

And see the commands I run to get the application successfully deployed

C:\rubytest\regexinheroku2>bundle install
...

C:\rubytest\regexinheroku2>git init
Initialized empty Git repository in C:/rubytest/regexinheroku2/.git/

C:\rubytest\regexinheroku2>git add -A
warning: LF will be replaced by CRLF in Gemfile.lock.
The file will have its original line endings in your working directory.

C:\rubytest\regexinheroku2>git commit -m "sddsfsdfsd"
[master (root-commit) 12cf382] sddsfsdfsd
warning: LF will be replaced by CRLF in Gemfile.lock.
The file will have its original line endings in your working directory.
 4 files changed, 39 insertions(+)
 create mode 100644 Gemfile
 create mode 100644 Gemfile.lock
 create mode 100644 aaa.rb
 create mode 100644 config.ru

C:\rubytest\regexinheroku2>heroku create
path1 gitmisc done
Creating app... done,   abc
https://abc.herokuapp.com/ | ....

C:\rubytest\regexinheroku2>git push heroku master
....
remote: Verifying deploy... done.
To https://git.heroku.com/abc.git
 * [new branch]      master -> master

C:\rubytest\regexinheroku2>

对于所有错误,首先要做的是检查日志。日志应该(几乎)总是提供比面向 public 的通用 "internal server error" 更好的错误线索。

但是,在这种情况下,我几乎可以肯定问题是您的本地计算机正在使用 ruby 版本 >= 2.4.0(可能 2.5.1,哪个是最新的?),而 heroku 是 运行 ruby 版本 2.3.7。来自 the documentation:

If your Gemfile does not contain a ruby entry, you will get MRI 2.3.7 on Cedar-14 and Heroku-16 stacks. On Heroku-18 you will get MRI 2.4.4.

要解决此问题,我建议包括:

ruby '2.5.1'

(或您使用的任何版本)在 Gemfile 中。这通常是一个很好的做法,因为它可以确保您的本地环境和生产环境相同。

具体来说,这里的实际问题是 Ruby 2.4.0 added the method Regexp#match?。所以 Heroku 目前正在抛出 "unknown method" 错误。