Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonatas/1dd29e55365bf340223dec102a49cfe0 to your computer and use it in GitHub Desktop.
Save jonatas/1dd29e55365bf340223dec102a49cfe0 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
def experimental_spec(file, occurrence)
parts = file.split('/')
dir = parts[0..-2]
filename = "experiment_#{occurrence}_#{__FILE__.split('.').first}_#{parts[-1]}"
File.join(*dir, filename)
end
def experiment(file, expression, replacement, index = nil)
new_content = Fast.replace_file file, expression, ->(node, _) do
if index.nil?
experiment(file, expression, replacement, match_index)
elsif match_index == index
instance_exec(node, &replacement)
end
end
if new_content
File.open(experimental_spec(file, index), 'w+') { |f| f.puts new_content }
[index, new_content]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment