Skip to content

Instantly share code, notes, and snippets.

@Arkham
Created March 12, 2013 08:47
Show Gist options
  • Save Arkham/5141240 to your computer and use it in GitHub Desktop.
Save Arkham/5141240 to your computer and use it in GitHub Desktop.
serialized replace
def serialized_replace!(source_field, dest_field)
length_delta = source_field.length - dest_field.length
sql_content.gsub!(/s:(\d+):([\\'"]+)(.*?)\2;/) do |match|
length = $1.to_i
delimiter = $2
string = $3
string.gsub!(/#{Regexp.escape(source_field)}/) do |match|
length -= length_delta
dest_field
end
%(s:#{length}:#{delimiter}#{string}#{delimiter};)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment