nobu.nokada / softhome.net wrote: > > Shared file descriptors share the position even between > processes. You can duplicate DATA. > Yes I know, so the first thing I tried was def extract(dest,name,mode) data = DATA.dup User.asroot { start=0 fn = path(dest,name) File.open(fn,File::CREAT|File::WRONLY,mode) { |file| data.rewind data.each { |line| if start==0 start=1 if line =~ %r{^=begin #{name} -*} else break if line =~ %r{^=end -*} yield line if block_given? file.puts line end } } } end Which didn't work. Futher investigation proved to be confusing. Can anyone explain whats going on? (I've appended the output from each line to make it easier) #!/bin/ruby -w #Hello cat #Hello dog #hello canary puts DATA.gets #-> one Ok, as expected. puts DATA.gets #-> two Ok, as expected. a=DATA.dup puts a.gets #-> nil Huh? I was expecting 'three' a.rewind # Try rewinding... puts a.gets #-> #!/bin/ruby -w Ok, I can live with that b=DATA.dup puts b.gets #-> nil Hmmm. Ok, same as before b.rewind # Rewind... puts b.gets #-> #!/bin/ruby -w As expected # Mix it up a bit... puts a.gets #-> #Hello cat Ok puts a.gets #-> #Hello dog Yep puts b.gets #-> #Hello cat Ok puts a.gets #-> #Hello canary Great! # Ok, lets simplify it a bit c=DATA.dup c.rewind d=DATA.dup d.rewind puts c.gets #-> #!/bin/ruby -w As expected puts d.gets #-> nil What the hell is going on here? # Try again, reordering stuff a bit... c=DATA.dup d=DATA.dup c.rewind puts c.gets #-> #!/bin/ruby -w As expected d.rewind puts d.gets #-> #!/bin/ruby -w It works! But why??? __END__ one two three