2011/11/29 Marc Heiler <shevegen / linuxmail.org>: > Hi. > > Consider you have two ruby files. > > file1.rb: > --------------------------------------------------- > require 'pp' > RUBY_FILE = './file2.rb' > > begin > ¨ÂïáÒÕÂÙ߯ÉÌ> rescue NameError => error > pp error # Do not report the error here. Be silent. > end > > # Now we call the two methods defined in file2.rb > foo() > bar() > --------------------------------------------------- > > file2.rb: > --------------------------------------------------- > > def foo > ¨Âõô§Ôèééó æòïí æï﨩§ > end > > # Create an error on purpose here. > joe_doe() > > def bar # The second method. > ¨Âõô§Ôèééó æòïí âáò¨©§ > end > --------------------------------------------------- > > Note - I use the parens () at bar() to make it more > explicit and illustrate my question. > > If you run file1.rb, an error occurs: > > undefined method `bar' for main:Object (NoMethodError) > > > The method call to foo() works. > > If you look at the content of file2.rb, you can see that > the method definition first has foo(), then a method > call to a method that does not exist (on purpose), and > then the method definition to bar() > > Ruby apparently stops processing file2.rb when it > encounters a NameError exception. > > My question is: > > - Is there a way to force or otherwise cause Ruby to continue > reading the second file? I am in control of the ruby files so > I can use eval without a problem. > > What I would like to achieve is to let ruby read a file > and treat it as a ruby file, but if it encounters errors, > it would disregard these errors, and continue processing > them. > > In a way, I'd need a faulty ruby loader that ignores errors > when instructed. > > Right now, in the code above, I can not achieve this, as > ruby stops the very moment it encounters an invalid method > call. How could I continue to process that faulty file? You could hook into Object#method_missing and Object.const_missing to handle NameErrors before they happen, although I'd try to redesign my code in any possible way before resorting to such methods. -- Stefan