On 8/9/05, Berger, Daniel <Daniel.Berger / qwest.com> wrote:
> 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.
> 
> 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?

Parse #caller[0]? Use __FILE__ and __LINE__?

  def foo
    c0 = caller[0].match(/^(.+):(\d+)(?::in `.*)?$/)
    c0 = c0.captures if c0
    data = File.open(c0[0], "rb") { |f| f.read }
    data = data.split($/)
    data = data[(c0[1].to_i -1) .. -1]
    p data
  end
  def bar
    foo
  end

  puts "Hello, world."
  foo
  bar
  puts "Goodbye, world."

-austin
-- 
Austin Ziegler * halostatue / gmail.com
               * Alternate: austin / halostatue.ca