尝试将 Rails 应用推送到 Heroku 时遇到奇怪的错误:
\n remote: -----> Building on the Heroku-20 stack\n remote: -----> Using buildpack: heroku/ruby\n remote: -----> Ruby app detected\n remote: -----> Installing bundler 2.2.16\n remote: -----> Removing BUNDLED WITH version in the Gemfile.lock\n remote: -----> Compiling Ruby/Rails\n remote: -----> Using Ruby version: ruby-3.0.1\n remote: -----> Installing dependencies using bundler 2.2.16\n remote: Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4\n remote: /tmp/build_ae8e646d/bin/bundle:42:in `gemfile': undefined method `present?' for "/tmp/build_ae8e646d/Gemfile":String (NoMethodError)\n remote: Did you mean? prepend\n remote: from /tmp/build_ae8e646d/bin/bundle:49:in …Run Code Online (Sandbox Code Playgroud) 我正在做 Ruby 任务,即“你有一个数字数组。你的任务是对升序的奇数进行排序,但偶数必须在它们的位置上。零不是奇数,您不需要移动它。如果您有一个空数组,则需要返回它”。
决定拆分初始数组:将奇数推入另一个数组,对其进行排序并将偶数添加到哈希中,其中键为 num,其初始索引为值。之后,尝试将偶数从哈希插入到奇数数组,在偶数的初始索引处。因此,代码如下所示:
def sort_array(source_array)
even_nums = Hash.new
odd_nums = []
return source_array if source_array.length == 0
source_array.each_with_index {|n, ind| even_nums[n] = ind if n.even?}
source_array.select{|n| odd_nums.push(n) if n.odd?}
odd_nums.sort!
even_nums.each do |k, v|
odd_nums.insert(v, k)
end
odd_nums
end
Run Code Online (Sandbox Code Playgroud)
对于像 [5, 3, 2, 8, 1, 4, 11] 这样的小数组,它可以按预期工作,但是如果我传递更大的东西,例如 [84, -64, 40, 53, 5, 88, 2, 14, 29, -79, -44, -23, 20, -67, -12, 28, -28, -37, -27, -62, -54, 93, -61, 50, 65, -63, -62, 77, -16, 49, …