From: James Adams Date: 2011-11-29T23:29:18+09:00 Subject: Re: Loading a faulty ruby file - forcing this ---20102908-186538638-1322576565=:56120 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable unsubscribe=0A=0A=0A________________________________=0A From: Robert Klemme= =0ATo: ruby-talk ML = =0ASent: Tuesday, November 29, 2011 8:56 AM=0ASubject: Re: Loading a faulty= ruby file - forcing this=0A =0AOn Tue, Nov 29, 2011 at 11:38 AM, Marc Heil= er wrote:=0A> Hi.=0A>=0A> Consider you have two ru= by files.=0A>=0A> file1.rb:=0A> -------------------------------------------= --------=0A> require 'pp'=0A> RUBY_FILE =3D './file2.rb'=0A>=0A> begin=0A> = =A0load RUBY_FILE=0A> rescue NameError =3D> error=0A> =A0# pp error # Do no= t report the error here. Be silent.=0A> end=0A>=0A> # Now we call the two m= ethods defined in file2.rb=0A> foo()=0A> bar()=0A> ------------------------= ---------------------------=0A>=0A> file2.rb:=0A> -------------------------= --------------------------=0A>=0A> def foo=0A> =A0puts 'This is from foo()'= =0A> end=0A>=0A> # Create an error on purpose here.=0A> joe_doe()=0A>=0A> d= ef bar # The second method.=0A> =A0puts 'This is from bar()'=0A> end=0A> --= -------------------------------------------------=0A>=0A> Note - I use the = parens () at bar() to make it more=0A> explicit and illustrate my question.= =0A>=0A> If you run file1.rb, an error occurs:=0A>=0A> undefined method `ba= r' for main:Object (NoMethodError)=0A>=0A>=0A> The method call to foo() wor= ks.=0A>=0A> If you look at the content of file2.rb, you can see that=0A> th= e method definition first has foo(), then a method=0A> call to a method tha= t does not exist (on purpose), and=0A> then the method definition to bar()= =0A>=0A> Ruby apparently stops processing file2.rb when it=0A> encounters a= NameError exception.=0A=0AOf course.=A0 An exception always immediately st= ops execution at the=0Apoint where it is thrown and unwinds the stack until= it finds a=0Ahandler (or the process terminates).=0A=0A> My question is:= =0A>=0A> - Is there a way to force or otherwise cause Ruby to continue=0A> = reading the second file? I am in control of the ruby files so=0A> I can use= eval without a problem.=0A=0ABasically you could wrap every individual sec= tion with "begin rescue=0Aend".=A0 Then such a section would be the smalles= t part which could=0Afail.=A0 Alternatively you could split the file in mul= tiple parts and=0Ahave a specific implementation of "load" which ignores er= rors:=0A=0Adef load_ignorant(s)=0A=A0 load(s)=0Arescue Exception=0A=A0 =A0 = # eat it=0Aend=0A=0A> What I would like to achieve is to let ruby read a fi= le=0A> and treat it as a ruby file, but if it encounters errors,=0A> it wou= ld disregard these errors, and continue processing=0A> them.=0A>=0A> In a w= ay, I'd need a faulty ruby loader that ignores errors=0A> when instructed.= =0A>=0A> Right now, in the code above, I can not achieve this, as=0A> ruby = stops the very moment it encounters an invalid method=0A> call. How could I= continue to process that faulty file?=0A=0AWhy do you want to do that?=A0 = That sounds like a bad plan to me since=0Ayou won't notice any issues and y= our application's state is unclear.=0A=0AKind regards=0A=0Arobert=0A=0A-- = =0Aremember.guy do |as, often| as.you_can - without end=0Ahttp://blog.rubyb= estpractices.com/ ---20102908-186538638-1322576565=:56120--