Skip to content

Instantly share code, notes, and snippets.

@marioaquino
Created March 18, 2013 02:08
Partially immutability in object construction in Ruby.
class Profile
def initialize(email)
self.class.send :define_method, :email, -> { email }
end
end
p = Profile.new 'foo@bar.com'
p.email #=> 'foo@bar.com'
p.email = 'hey@now.com' #=> <boom>
p.instance_variable_set :@email, 'hey@now.com'
p.email #=> 'foo@bar.com'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment