K
konsu
hello,
i need to set an instance variable in my object once and make sure this
variable is not changed afterwards. i tried the code below which i expected
to fail with a TypeError but it happily outputs '2'. i kind of understand
why it succeeds, i think it just creates another @id when i call id=()
method, but is there a way to do what i want to do?
thanks
konstantin
class C
attr_accessor :id
def initialize(id)
@id = id
@id.freeze
end
end
c = C.new(1)
c.id = 2
puts c.id
i need to set an instance variable in my object once and make sure this
variable is not changed afterwards. i tried the code below which i expected
to fail with a TypeError but it happily outputs '2'. i kind of understand
why it succeeds, i think it just creates another @id when i call id=()
method, but is there a way to do what i want to do?
thanks
konstantin
class C
attr_accessor :id
def initialize(id)
@id = id
@id.freeze
end
end
c = C.new(1)
c.id = 2
puts c.id