Skip to content

Instantly share code, notes, and snippets.

@mpasternacki
Created April 15, 2011 17:55
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 mpasternacki/922132 to your computer and use it in GitHub Desktop.
Save mpasternacki/922132 to your computer and use it in GitHub Desktop.
Add g method (named after (s)ed's g//) to Chef::Util::FileEdit for more flexible text manipulation
class Chef
class Util
class FileEdit
public
# Run block for matching lines, passing it matching line.
# Replaces matching lines with value returned by the block.
def g(regex)
exp = Regexp.new(regex)
new_contents = []
contents.each do |line|
if line =~ exp
replacement = (yield line) || line
new_contents << replacement
@file_edited ||= ( line != replacement )
else
new_contents << line
end
@contents = new_contents
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment