Freezing OpenStruct

The irb session below demonstrates that OpenStruct objects (part of
the standard library) allow modification after a #freeze. Is this
desirable?

Gavin

require ‘ostruct’
=> true
s = OpenStruct.new
=>
s.foo = 25
=> 25
s
=>
s.freeze
=>
s.foo = 6
=> 6
s
=>

Hi,

···

In message “Freezing OpenStruct” on 03/08/26, Gavin Sinclair gsinclair@soyabean.com.au writes:

The irb session below demonstrates that OpenStruct objects (part of
the standard library) allow modification after a #freeze. Is this
desirable?

I forgot to check frozen status. Thanks.

						matz.

It don’t think so. A quick fix would be that:

Index: ostruct.rb

···

On 2003-08-26 21:39:34 +0900, Gavin Sinclair wrote:

The irb session below demonstrates that OpenStruct objects (part of
the standard library) allow modification after a #freeze. Is this
desirable?

===================================================================
RCS file: /src/ruby/lib/ostruct.rb,v
retrieving revision 1.6
diff -u -p -r1.6 ostruct.rb
— ostruct.rb 18 Nov 2002 20:09:46 -0000 1.6
+++ ostruct.rb 26 Aug 2003 15:05:08 -0000
@@ -38,6 +38,11 @@ class OpenStruct
@table.delete name.to_sym
end

  • def freeze
  • super
  • @table.freeze
  • end
  • def inspect
    str = “<#{self.class}”
    for k,v in @table


Simplicity is prerequisite for reliability. [Handwritten annotation]
– Edsger Dijkstra, How do we tell truths that might hurt?