Created
November 18, 2013 22:47
-
-
Save nedzadarek/7536766 to your computer and use it in GitHub Desktop.
What happen when you freeze object? Or variable?
This file contains 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 U | |
def initialize | |
@uu='cdefaflejlfkj' | |
end | |
attr_accessor :uu | |
end | |
u=U.new.freeze | |
u.uu | |
# => "cdefaflejlfkj" # first letter is small "c" | |
u.uu=2 # we cannot do this, ok | |
# RuntimeError: can't modify frozen U | |
# from (pry):93:in `__pry__' | |
u.uu.capitalize! | |
# => "Cdefaflejlfkj" | |
u.uu | |
=> "Cdefaflejlfkj" # capitalized! | |
u.uu.freeze | |
# "Cdefaflejlfkj" | |
u.uu.reverse! | |
# RuntimeError: can't modify frozen String | |
# from (pry):105:in `reverse!' | |
u.uu | |
#=> "Cdefaflejlfkj" # still the same |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment