Skip to content

Instantly share code, notes, and snippets.

@amiryal
Created February 7, 2016 15:21
Show Gist options
  • Save amiryal/fb1b2998354797ddead9 to your computer and use it in GitHub Desktop.
Save amiryal/fb1b2998354797ddead9 to your computer and use it in GitHub Desktop.
Ruby method to keep track of files written to while executing given block
class WrittenFilesTracking
def self.written_files
@@written_files
end
def self.start_with
@@start_with
end
def self.with(start_with:'./')
@@start_with = start_with
@@written_files = []
begin
class << File
alias_method :__written_files_open, :open
def __written_files_tracing_open(*args, &block)
if args[0].start_with?(WrittenFilesTracking.start_with) && args[1] =~ /w/
WrittenFilesTracking.written_files.push(args[0])
end
__written_files_open(*args, &block)
end
alias_method :open, :__written_files_tracing_open
end
yield
ensure
class << File
alias_method :open, :__written_files_open
remove_method :__written_files_open
remove_method :__written_files_tracing_open
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment