Created
May 29, 2009 21:52
-
-
Save dmerrick/120238 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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