Forum: Ruby Frozen OpenStruct allows modification

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
157e2cb5b1e7b008769a7e2c11ae5b66?d=identicon&s=25 bhz (Guest)
on 2009-02-14 13:35
(Received via mailing list)
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
40613e55d7082e5f08429dfb50d0680e?d=identicon&s=25 Stefan Lang (Guest)
on 2009-02-15 00:22
(Received via mailing list)
2009/2/14 bhz <bhznospam@yahoo.com>:
> 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-15 01:01
(Received via mailing list)
On 15.02.2009 00:18, Stefan Lang 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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-02-15 01:02
(Received via mailing list)
On Sun, Feb 15, 2009 at 12:18 AM, Stefan Lang
<perfectly.normal.hacker@gmail.com> 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
157e2cb5b1e7b008769a7e2c11ae5b66?d=identicon&s=25 bhz (Guest)
on 2009-02-15 10:45
(Received via mailing list)
On Sun, 15 Feb 2009 00:59:20 +0100, Robert Klemme wrote:

>>> a.foo = 20
>
>   robert

Agreed.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-02-15 11:40
(Received via mailing list)
On Sun, Feb 15, 2009 at 10:43 AM, bhz <bhznospam@yahoo.com> 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-15 18:10
(Received via mailing list)
On 15.02.2009 11:38, Robert Dober wrote:
> On Sun, Feb 15, 2009 at 10:43 AM, bhz <bhznospam@yahoo.com> 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
157e2cb5b1e7b008769a7e2c11ae5b66?d=identicon&s=25 bhz (Guest)
on 2009-02-17 09:35
(Received via mailing list)
On Sun, 15 Feb 2009 05:38:10 -0500, Robert Dober wrote:

>
> Cheers
> Robert

Thanks for input. So we can expect it to be fixed in the new release?
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-02-17 12:44
(Received via mailing list)
On Tue, Feb 17, 2009 at 9:34 AM, bhz <bhznospam@yahoo.com> 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.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-03-02 12:58
(Received via mailing list)
They fixed it :)
R
This topic is locked and can not be replied to.