我发现,如果代码在Rails executor以内运行,则无法识别精化.这似乎不是因为阻挡或触发,这是我唯一的猜测.以下是一些演示代码:

require './config/environment.rb'

module ExammpleRefinement
  refine ::Kernel do
    def say_hello
      puts "hello"
    end
  end
end

using ExammpleRefinement

puts "alone"
say_hello
puts "block"
loop do
  say_hello
  break
end
puts "proc"
Proc.new{say_hello}.call

puts "rails executor"

::Rails.application.executor.wrap do
  say_hello
end

ruby example.rb运行(不能用rails runner运行,因为从rails 7开始,它将整个脚本包装在一个执行器中,所以它 destruct 了实验)

alone
hello
block
hello
proc
hello
rails executor
Traceback (most recent call last):
    2: from example.rb:25:in `<main>'
    1: from ....rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activesupport-7.0.4/lib/active_support/execution_wrapper.rb:92:in `wrap'
scripts/example.rb:26:in `block in <main>': undefined local variable or method `say_hello' for main:Object (NameError)

推荐答案

我无法使用您提供的确切测试文件重现行为(在我的测试中,它在需要config/environment个测试文件后立即失败,不管执行器包装如何),但我想我已经跟踪到了相同的原因:

在应用程序初始化期间,可能是由于使用了EventedFileUpdateChecker,Kernel gets prepended by ForkTracker.

想必(也有些情有可原),精细化并不喜欢这样.

Ruby-on-rails相关问答推荐

发送不调用类或模块中的私有方法

是否可以使用 Turbo 帧更新 Rails 表单输入字段值?

我如何在不在 gemfile 中的 rake 任务中要求 gem?

Rails 7,Hotwire 动态表单,更新值不起作用

ruby on rails 中的自定义关联

Mongoid 3 + Heroku (MongoHQ) 导致 Moped::Errors::OperationFailure

未初始化的常量 ActionDispatch::Session::EncryptedCookieStore (NameError)

基于 2 列定义唯一主键

如何避免 RSpec 3.0 中 stub_chain 的弃用警告?

Rails 3:如何正确显示textarea中的文本?

rails 模型 has_many 本身

Rails ActiveRecord 查询日期范围

设计和强大的参数

如何在Ruby 中对数字进行上限和舍入

Test::Unit Rails - 如何断言一个数字大于另一个?

如何一次显示一条 Ruby on Rails 表单验证错误消息?

资源和资源方法之间的区别

Ruby on Rails:如何在 select_tag 中使用默认占位符?

Sidekiq Rails 4.2 使用 Active Job 还是 Worker?有什么不同

设置EDITOR或BUNDLER_EDITOR环境变量