On 09/08/05, Berger, Daniel <Daniel.Berger / qwest.com> 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
> 
> 
> 
> 
> 
Hello Dan,

maybe something like this:

bschroed@black:~/svn/projekte/ruby-things$ cat slurpself.rb 
def tail_from(file, line)
  File.readlines(file)[line..-1]
end

puts "Hello World"
p tail_from(__FILE__, __LINE__)
puts "How's it going?"
puts "Just fine"
bschroed@black:~/svn/projekte/ruby-things$ ruby slurpself.rb 
Hello World
["puts \"How's it going?\"\n", "puts \"Just fine\"\n"]
How's it going?
Just fine

regards,

Brian

-- 
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/