Created
June 16, 2009 21:34
-
-
Save tenderlove/130915 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
#!/usr/bin/ruby -w | |
def ensure_data_file | |
unless defined? DATA | |
const_set(:DATA, File.open($PROGRAM_NAME, 'r+')) | |
DATA.seek 0, IO::SEEK_END | |
DATA.puts | |
DATA.puts('__END__') | |
end | |
DATA | |
end | |
module AaronsIOMeow | |
def reopen io_or_path = self.path, mode = 'r+' | |
pos = self.pos | |
new_data = super | |
new_data.seek pos | |
new_data | |
end | |
def db default = {} | |
reopen | |
start_position = self.pos | |
db = Marshal.load(read) rescue default | |
seek start_position | |
yield db | |
write Marshal.dump(db) | |
truncate pos | |
end | |
end | |
ensure_data_file | |
DATA.extend(AaronsIOMeow) | |
DATA.db(Hash.new 0) do |db| | |
db[:key] += 1 | |
p db | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment