Skip to content

Instantly share code, notes, and snippets.

@skatkov
Last active January 29, 2018 16:29
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 skatkov/662fd1cfd76543da3afe0cf22a3b7a6e to your computer and use it in GitHub Desktop.
Save skatkov/662fd1cfd76543da3afe0cf22a3b7a6e to your computer and use it in GitHub Desktop.
String removal from malloc
class String
def auto_clear
begin
result = yield self
result.equal?(self) ? nil : result # avoid leaking
ensure
clear
end
end
end
#Then people can do this with Strings whose lifetime they know.
expr_returning_string.auto_clear do |str|
puts "We got #{str}."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment