简体   繁体   中英

replacing `instance_exec(&Proc.new)` in Ruby 2.7

We have a bit of long-lived code in our app that sets up a "feature flag" system and looks like this:

    def configure
      Module.new do
        def self.feature(name, options = {})
          Features.current.add(name, options)
        end
        instance_exec(&Proc.new)
      end
      self
    end

In Ruby 2.7, that instance_exec() bit throws a nasty deprecation warning. How would I fix in this case? Simply replacing with &block as it suggests doesn't work here.

thanks, everyone. @tadman had it right. I had replaced the &Proc.new with &block but was a dummy and didn't add that to the method definition also. Adding in both places solved the issue and eliminated the ruby 2.7 deprecation warning (about not using Proc.new to capture a block.)

The corrected code:

def configure(&block)
  Module.new do
    def self.feature(name, options = {})
      Features.current.add(name, options)
    end
    instance_exec(&block)
  end
  self
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM