我发现,如果代码在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)