Created
September 19, 2013 10:54
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dkubb Mayte that would make sense for adamantium? Dunno a use case for myself, because I normally use
Anima#update
.