Frozen OpenStruct allows modification

Discussion in 'Ruby' started by bhz, Feb 14, 2009.

  1. bhz

    bhz Guest

    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
    bhz, Feb 14, 2009
    #1
    1. Advertising

  2. bhz

    Stefan Lang Guest

    2009/2/14 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


    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
    Stefan Lang, Feb 14, 2009
    #2
    1. Advertising

  3. On 15.02.2009 00:18, Stefan Lang wrote:
    > 2009/2/14 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

    >
    > 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
    Robert Klemme, Feb 14, 2009
    #3
  4. bhz

    Robert Dober Guest

    On Sun, Feb 15, 2009 at 12:18 AM, Stefan Lang
    <> 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

    --
    It is change, continuing change, inevitable change, that is the
    dominant factor in society today. No sensible decision can be made any
    longer without taking into account not only the world as it is, but
    the world as it will be ... ~ Isaac Asimov
    Robert Dober, Feb 15, 2009
    #4
  5. bhz

    bhz Guest

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

    > On 15.02.2009 00:18, Stefan Lang wrote:
    >> 2009/2/14 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

    >>
    >> 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


    Agreed.
    bhz, Feb 15, 2009
    #5
  6. bhz

    Robert Dober Guest

    On Sun, Feb 15, 2009 at 10:43 AM, bhz <> 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



    --
    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)
    Robert Dober, Feb 15, 2009
    #6
  7. On 15.02.2009 11:38, Robert Dober wrote:
    > On Sun, Feb 15, 2009 at 10:43 AM, bhz <> 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
    Robert Klemme, Feb 15, 2009
    #7
  8. bhz

    bhz Guest

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

    > On Sun, Feb 15, 2009 at 10:43 AM, bhz <> 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


    Thanks for input. So we can expect it to be fixed in the new release?
    bhz, Feb 17, 2009
    #8
  9. bhz

    Robert Dober Guest

    On Tue, Feb 17, 2009 at 9:34 AM, bhz <> wrote:
    > On Sun, 15 Feb 2009 05:38:10 -0500, Robert Dober wrote:
    >
    >> On Sun, Feb 15, 2009 at 10:43 AM, bhz <> 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

    >
    > 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)
    Robert Dober, Feb 17, 2009
    #9
  10. bhz

    Robert Dober Guest

    They fixed it :)
    R
    Robert Dober, Mar 2, 2009
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. est
    Replies:
    1
    Views:
    541
  2. LC Geldenhuys
    Replies:
    5
    Views:
    249
    Robert Klemme
    Feb 18, 2004
  3. Jamis Buck

    OpenStruct respond_to? problem

    Jamis Buck, Aug 24, 2004, in forum: Ruby
    Replies:
    17
    Views:
    241
    Mauricio Fernández
    Aug 25, 2004
  4. Hal Fulton

    OpenStruct and #send

    Hal Fulton, Oct 27, 2004, in forum: Ruby
    Replies:
    7
    Views:
    160
    Florian Gross
    Oct 27, 2004
  5. trans.  (T. Onoma)

    OpenStruct#update ?

    trans. (T. Onoma), Nov 14, 2004, in forum: Ruby
    Replies:
    70
    Views:
    736
    trans. (T. Onoma)
    Nov 19, 2004
Loading...

Share This Page