Skip to content

Instantly share code, notes, and snippets.

@lunks
Forked from viniciussbs/attr_reader_test.rb
Created September 7, 2014 18:56
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 lunks/8d55b01376b591c2dd1c to your computer and use it in GitHub Desktop.
Save lunks/8d55b01376b591c2dd1c to your computer and use it in GitHub Desktop.
class Person
def initialize
@attributes = {}
end
def attributes
@attributes.dup.freeze
end
def name
@attributes[:name]
end
end
john_doe = Person.new
# #<Person:0x007fa713c56d60 @attributes={}>
john_doe.attributes
# {}
john_doe.attributes[:name] = "John Doe"
# RuntimeError: can't modify frozen Hash
john_doe.name
# nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment