How to underestimate the required Ruby file?

file1 requires file2 , and I want to be able to undo the evaluation of file2 under certain conditions without leaving the whole process.

 # file1.rb puts "In file 1" require 'file2' puts "Back in file 1" # file2.rb puts "In file 2" # return if some_conditional puts "Still in file 2" 

When running file1 output I want to see is:

 In file 1 In file 2 Back in file 1 

The goal is that Still in file 2 never prints, and Back in file 1 prints. Can I do something in file2 to make this possible?

I can not use exit / exit! / abort here because Back in file 1 will never be printed. I could use raise / fail , but for this I would have to change file1 and rescue failed require . I hope to find a way that does not involve changing file1 .

UPDATE :

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

+10
ruby return exit raise abort
source share
4 answers

UPDATE :

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

ORIGINAL :

Commentator Matt noted that the Feature 4840, which will do exactly what I ask, has been discussed since June 2011 . In addition, this feature was still discussed in November 2015 at major group meetings on new Ruby features.

There are many difficulties associated with the development of such a function; For a list of pros and cons, I highly recommend checking out the discussions.

The proposed function will allow you to exit the required file when using any of the following top-level operators:

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

And this will 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 this function remains unfulfilled, I awarded Stinslag a reward for successfully solving the problem (as it was originally written) of the letter, if not the spirit.

+2
source share

Lines below __END__ will not be executed .

 # file2.rb puts "In file 2" __END__ puts "Still in file 2" # Never gets called 
+5
source share

I do not know any official method for exiting the required files, especially since there are several require methods (for example, patches are required for patches)

The best solution I could come up with was to use a ruby ​​emission control flow. I'm not sure what you are using to determine if you should return early, but this should be able to handle 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 
+1
source share

Is it possible to use the method? It will still parse the method, but will not be 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
source share

All Articles