How to cancel evaluating a required Ruby file? (a.k.a. top-level return)

后端 未结 4 971
不知归路
不知归路 2021-01-17 19:14

file1 requires file2, and I want to be able to cancel evaluating file2 under certain conditions without exiting the whole process.

相关标签:
4条回答
  • 2021-01-17 19:26

    Is using a method possible ? It will still parse the method but won't get executed. Something like :

    #file1.rb
      puts "In file 1"
      require 'file2'
      puts "Back in file 1"
      a_method
    
    #file2.rb
      puts "In file 2"
      # <= A
    
      def a_method
        puts "Still in file 2"
      end
    
    0 讨论(0)
  • 2021-01-17 19:30

    UPDATE:

    A "top-level return" feature has been added.

    ORIGINAL:

    Commenter matt pointed out that Feature 4840, which would do exactly what I'm asking about, has been in discussion since June 2011. Further, the feature was still being discussed as late as November 2015 in core team meetings regarding new Ruby features.

    There are a lot of difficulties involved in designing a feature like this; for a list of the pros and cons, I highly suggest checking out the discussions.

    The proposed feature would allow exiting the required file while using any of the the following top-level statements:

    if condition
      return
    end
    
    while condition
      # ...
      return
    end
    
    begin
      # ...
      return
    rescue
      # ...
      return
    ensure
      # ...
      return
    end
    

    And it would not exit the required file in the following statements:

    class Foo
      return # LocalJumpError
    end
    
    def foo
      return # returns from method, not from required file
    end
    
    proc do
      return # LocalJumpError
    end
    
    x = -> { return } # returns as from lambda, not from required file
    

    Since the feature remains unimplemented, I have awarded the bounty to steenslag for successfully solving the problem (as originally written) to the letter, if not the spirit.

    0 讨论(0)
  • 2021-01-17 19:34

    Lines below __END__ will not be executed.

    # file2.rb
    puts "In file 2" 
    __END__
    puts "Still in file 2" # Never gets called
    
    0 讨论(0)
  • 2021-01-17 19:43

    I don't know of any official method for breaking out of required files, especially as there are several require methods (e.g., bundler monkey patches require)

    The best solution I could come up with was using rubys throw-catch control flow. I'm not sure conditional you're using to determine if execute should return early, but this should be able to cope with most situations

    # file1.rb
    puts "In file 1" 
    catch(:done) do
        require 'file2' end
    puts "Back in file 1"
    
    # file2.rb
    puts "In file 2" 
    throw :done
    puts "Still in file 2" # Never gets called
    
    0 讨论(0)
提交回复
热议问题