From: Gennady Date: 2004-04-08T02:56:05+09:00 Subject: Re: line directives Andrew Johnson wrote: > Note: > I'm replying to my own post as I found the following response in the > ruby-talk archives, but no replies seem to have made it through to my > c.l.r. newsfeed. > > > In [ruby-talk:96620] (http://ruby-talk.org/blade/96620), Assaph writes: > > >>What exactly do you need it for? > > > This would be useful when the source code has been extracted / assembled > from another source --- in particular, literate programming. When the > extracted code is run, it is much nicer when warnings and errors point > to the relevant locations in the literate source file (which is where the > code is written and edited), rather than the extracted source file which > is executed. > > regards, > andrew > If you execute your extracted code via 'eval' you can indicate a file name and a starting line number that 'eval' will use to report errors: ----------------------------------------------------------- Kernel::eval eval( aString [, aBinding [file [line]]]) -> anObject ------------------------------------------------------------------------ Evaluates the Ruby expression(s) in aString. If aBinding is given, the evaluation is performed in its context. The binding may be a Binding object or a Proc object. If the optional file and line parameters are present, they will be used when reporting syntax errors. As of Ruby 1.8, local variables assigned within an eval are only available after the eval if they were defined at the outter scope before the eval executed. In this way eval has the same scoping rules as blocks. def getBinding(str) return binding end str = "hello" eval "str + ' Fred'" #=> "hello Fred" eval "str + ' Fred'", getBinding("bye") #=> "bye Fred" Gennady.