From: Ezra Zygmuntowicz Date: 2005-08-10T01:15:02+09:00 Subject: Re: Re-reading a portion of a source file from a specific point On Aug 9, 2005, at 8:14 AM, Berger, Daniel wrote: > Hi all, > > >> From within a Ruby program, is there a way to get Ruby to re-read >> itself >> > from a specific part of the file? Let me explain. Say I have a > method > "foo". I would like "foo" to slurp the rest of the script into memory > and write it to a file, possibly a temp file. > > For example: > > 1: puts "Hello World" > 2: foo() > 3: puts "How's it going?" > > In this script I would like lines 2 and 3 (or possibly just line 3 > - I'm > not sure yet), but NOT line 1, read into memory and written to a file > (by the foo method). I thought perhaps there were some tricks I could > use with __DATA__, __END__ or __FILE__, but I wasn't sure. > > Any ideas? > > Regards, > > Dan > > > > > There is an obscure constant called SCRIPT_LINES__ . To use it you would have to put the script that you want to have access to in a file and include it in another. Like so: -----test.rb-------- a= "Hello World" b = "Foo Bar" c = "How's it going?" d = "Another Line" ------end test.rb------ ------main.rb------- SCRIPT_LINES__ = {} require 'test' p SCRIPT_LINES__ p SCRIPT_LINES__.keys -----end main.rb-------- Result: ezra:~/Sites ez$ ruby main.rb {"./main.rb"=>["a= \"Hello World\"\n", "b = \"Foo Bar\"\n", "c = \"How's it going?\"\n", "d = \"Another Line\""]} ------------ ["./main.rb"] So after you do this, you will have a hash that contains arrays of every line in every file you require, keyed by the file name of the required file. Kind of wierd but I think you could use this to do what you want to do. HTH- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster 509-577-7732 ezra@yakima-herald.com