Skip to content

Instantly share code, notes, and snippets.

@nedzadarek
Created November 18, 2013 22:47
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 nedzadarek/7536766 to your computer and use it in GitHub Desktop.
Save nedzadarek/7536766 to your computer and use it in GitHub Desktop.
What happen when you freeze object? Or variable?
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