Skip to content

Instantly share code, notes, and snippets.

@marioaquino
Created March 18, 2013 02:08
Show Gist options
  • Save marioaquino/5184525 to your computer and use it in GitHub Desktop.
Save marioaquino/5184525 to your computer and use it in GitHub Desktop.
Partially immutability in object construction in Ruby.
class Profile
def initialize(email)
self.class.send :define_method, :email, -> { email }
end
end
p = Profile.new '[email protected]'
p.email #=> '[email protected]'
p.email = '[email protected]' #=> <boom>
p.instance_variable_set :@email, '[email protected]'
p.email #=> '[email protected]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment