Frozen OpenStruct allows modification

B

bhz

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
 
S

Stefan Lang

2009/2/14 bhz said:
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
 
R

Robert Klemme

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
 
R

Robert Dober

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
 
R

Robert Dober

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



--
There are some people who begin the Zoo at the beginning, called
WAYIN, and walk as quickly as they can past every cage until they get
to the one called WAYOUT, but the nicest people go straight to the
animal they love the most, and stay there. ~ A.A. Milne (from
Winnie-the-Pooh)
 
R

Robert Klemme

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
 
B

bhz

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

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

Robert Dober

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.



--
There are some people who begin the Zoo at the beginning, called
WAYIN, and walk as quickly as they can past every cage until they get
to the one called WAYOUT, but the nicest people go straight to the
animal they love the most, and stay there. ~ A.A. Milne (from
Winnie-the-Pooh)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top