Skip to content

Instantly share code, notes, and snippets.

@txus
Created September 19, 2013 10:54
Show Gist options
  • Save txus/6621824 to your computer and use it in GitHub Desktop.
Save txus/6621824 to your computer and use it in GitHub Desktop.
Transients in Ruby. Transients are a way to mutate an immutable object for a limited scope of changes and then make it immutable again.
class Object
def transient(&block)
dup.tap(&block).freeze
end
end
str = "hey".freeze
new_str = str.transient do |mutable_str|
mutable_str.upcase!
end
p new_str == "HEY"
p new_str.frozen? == true
@mbj
Copy link

mbj commented Sep 19, 2013

@dkubb Mayte that would make sense for adamantium? Dunno a use case for myself, because I normally use Anima#update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment