Consider you have two ruby files.
RUBY_FILE = ‘./file2.rb’
rescue NameError => error
pp error # Do not report the error here. Be silent.
Now we call the two methods defined in file2.rb
puts ‘This is from foo()’
Create an error on purpose here.
def bar # The second method.
puts ‘This is from bar()’
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
In a way, I’d need a faulty ruby loader that ignores errors
when instructed (instructed by me, that is).
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?