Frozen OpenStruct allows modification

Why does OpenStruct allow modification after it has been frozen? Output
is
20 in both ruby 1.8.6 and 1.9:

require ‘ostruct’

a = OpenStruct.new()
a.foo = 10
a.freeze()
a.foo = 20
puts a.foo

2009/2/14 bhz [email protected]:

Why does OpenStruct allow modification after it has been frozen? Output is
20 in both ruby 1.8.6 and 1.9:

require ‘ostruct’

a = OpenStruct.new()
a.foo = 10
a.freeze()
a.foo = 20
puts a.foo

Because the OpenStruct instance is never modified here,
only the internal hash it uses to store attribute values.
The internal hash is not frozen, only the OpenStruct instance
itself.

Stefan

On 15.02.2009 00:18, Stefan L. wrote:

puts a.foo

Because the OpenStruct instance is never modified here,
only the internal hash it uses to store attribute values.
The internal hash is not frozen, only the OpenStruct instance
itself.

That could still be considered a bug and IMHO it is.

Kind regards

robert

On Sun, 15 Feb 2009 00:59:20 +0100, Robert K. wrote:

a.foo = 20

robert

Agreed.

On Sun, Feb 15, 2009 at 10:43 AM, bhz [email protected] wrote:
I have submitted a patch to ruby-core.

Turned out that this was certainly an error only already created
write accessors were ignoring the frozen? state of the object, thus

x =OpenStruct::new.freeze
x.a = 42

would have thrown the expected TypeError.

Cheers
Robert

On Sun, Feb 15, 2009 at 12:18 AM, Stefan L.
[email protected] wrote:

Because the OpenStruct instance is never modified here,
only the internal hash it uses to store attribute values.
The internal hash is not frozen, only the OpenStruct instance
Sounds like a bug to me.
Robert

On Sun, 15 Feb 2009 05:38:10 -0500, Robert D. wrote:

Cheers
Robert

Thanks for input. So we can expect it to be fixed in the new release?

On Tue, Feb 17, 2009 at 9:34 AM, bhz [email protected] wrote:

would have thrown the expected TypeError.

Cheers
Robert

Thanks for input. So we can expect it to be fixed in the new release?

I really do not know, depends if my patch or Joel’s will be accepted,
but I see no reason why not ;).
R.

On 15.02.2009 11:38, Robert D. wrote:

On Sun, Feb 15, 2009 at 10:43 AM, bhz [email protected] wrote:
I have submitted a patch to ruby-core.

Well done!

Turned out that this was certainly an error only already created
write accessors were ignoring the frozen? state of the object, thus

x =OpenStruct::new.freeze
x.a = 42

would have thrown the expected TypeError.

Thanks for the update and the insights!

Kind regards

robert

They fixed it :slight_smile:
R

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs