freeze string fails to freeze ???

Discussion in 'Ruby' started by jwaala.amplify@gmail.com, Mar 15, 2007.

  1. Guest

    >> a='asdf'
    => "asdf"
    >> a.freeze

    => "asdf"
    >> a='qwer'

    => "qwer"

    should'nt that throw and exception ???
     
    , Mar 15, 2007
    #1
    1. Advertising

  2. On 15.03.2007 16:33, wrote:
    >>> a='asdf'

    > => "asdf"
    >>> a.freeze

    > => "asdf"
    >>> a='qwer'

    > => "qwer"
    >
    > should'nt that throw and exception ???


    No, the *object* is frozen. This has no effects on the *variable*.

    >> a='asdf'

    => "asdf"
    >> a.freeze

    => "asdf"
    >> a << "foo"

    TypeError: can't modify frozen string
    from (irb):4:in `<<'
    from (irb):4
    from :0

    Kind regards

    robert
     
    Robert Klemme, Mar 15, 2007
    #2
    1. Advertising

  3. Le jeudi 15 mars 2007 16:35, a =E9crit=A0:
    > >> a=3D'asdf'

    >
    > =3D> "asdf"
    >
    > >> a.freeze

    >
    > =3D> "asdf"
    >
    > >> a=3D'qwer'

    >
    > =3D> "qwer"
    >
    > should'nt that throw and exception ???


    No, because calling the freeze method prevents from modifying the object, i=
    t=20
    doesn't prevent from modifying the variable. Remember that a variable is ju=
    st=20
    a way to give a name to an object, but it is not tied to this object foreve=
    r.

    So, your code doesn't raise an exception, but this one does :

    irb(main):001:0> a=3D"abc"
    =3D> "abc"
    irb(main):002:0> a.freeze
    =3D> "abc"
    irb(main):003:0> a << 'd'
    TypeError: can't modify frozen string
    from (irb):3:in `<<'
    from (irb):3

    What you are looking for is closer of a constant, actually :

    irb(main):004:0> A=3D"abc"
    =3D> "abc"
    irb(main):005:0> A=3D"def"
    (irb):5: warning: already initialized constant A

    =2D-=20
    Olivier Renaud
     
    Olivier Renaud, Mar 15, 2007
    #3
  4. wrote:
    >>> a='asdf'

    > => "asdf"
    >>> a.freeze

    > => "asdf"
    >>> a='qwer'

    > => "qwer"
    >
    > should'nt that throw and exception ???
    >


    freeze is a method on the object referenced by a,
    not on the variable a (a is just a name, not the
    object itself).

    a = 'abcd'
    a.freeze

    # a[2]='w' raises
    # ./freezetest.rb:7:in `[]=': can't modify frozen string (TypeError)

    This causes an exception because you are trying to modify the
    frozen object.

    a = 'defg'

    This does not, because the object (which up to now was
    referenced by a) is not changed. a now refers to a
    different object.

    Try it with a constant:

    A = 'abcd'
    A = 'defg' # raises a warning: already initialized constant A



    --

    Regards,

    Fergal Byrne - Technical Director


    Adnet: Web Builders to the Design Industry

    http://www.adnet.ie/ t:+353 1 855 8951 aim/skype:FergByrne

    === We've Moved! 63 Lower Gardiner Street, Dublin 1 ===
     
    Fergal J Byrne, Mar 15, 2007
    #4
  5. On 3/15/07, Olivier Renaud <> wrote:

    > No, because calling the freeze method prevents from modifying the object, it
    > doesn't prevent from modifying the variable. Remember that a variable is just
    > a way to give a name to an object, but it is not tied to this object forever.


    Is it just me or has the confusion between variables and objects been
    coming up more frequently than usual of late?

    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
     
    Rick DeNatale, Mar 15, 2007
    #5
  6. Le jeudi 15 mars 2007 23:16, Rick DeNatale a =E9crit=A0:
    > On 3/15/07, Olivier Renaud <> wrote:
    > > No, because calling the freeze method prevents from modifying the objec=

    t,
    > > it doesn't prevent from modifying the variable. Remember that a variable
    > > is just a way to give a name to an object, but it is not tied to this
    > > object forever.

    >
    > Is it just me or has the confusion between variables and objects been
    > coming up more frequently than usual of late?


    Not more often than the elsif/elseif confusion :D

    =2D-=20
    Olivier Renaud
     
    Olivier Renaud, Mar 15, 2007
    #6
    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. John Saunders
    Replies:
    1
    Views:
    685
    John Saunders
    Nov 18, 2003
  2. Earl Eiland
    Replies:
    3
    Views:
    532
    Peter Hansen
    May 16, 2005
  3. Trent Mick
    Replies:
    0
    Views:
    484
    Trent Mick
    May 16, 2005
  4. Replies:
    2
    Views:
    300
    Grant Wagner
    Dec 23, 2004
  5. dayo
    Replies:
    11
    Views:
    353
    Ilya Zakharevich
    Dec 16, 2005
Loading...

Share This Page