Skip to content

Instantly share code, notes, and snippets.

@dmerrick
Created May 29, 2009 21:52
Show Gist options
  • Save dmerrick/120238 to your computer and use it in GitHub Desktop.
Save dmerrick/120238 to your computer and use it in GitHub Desktop.
class Array
# This method reads from the DATA section of this file, which
# in Ruby is marked by __END__. It takes each line and pushes
# it into the calling array, yields the code block and writes
# the array, one element per line, to the end of the file,
# starting at the beginning of the DATA block. Idea stolen
# from O'Reilly's Perl Cookbook.
def tie()
DATA.each_line { |line| self.push(line.chomp) }
yield
# open the file for writing
# TODO: use "a" instead of "w+"
File.open(__FILE__,File::RDWR) do |file|
# look through the file for the DATA section
file.each_line do |line|
break if line =~ /^__END__$/
end
# write to the file
each { |line| line ? file.puts(line) : file.puts("") }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment