>From: Anders Bengtsson <ndrsbngtssn / yahoo.se> >Reply-To: ruby-talk / netlab.co.jp >To: ruby-talk / netlab.co.jp (ruby-talk ML) >Subject: [ruby-talk:8931] String confusion >Date: Wed, 10 Jan 2001 01:09:57 +0900 > >Hello everyone, > >I just ran into a problem with Strings that really confuses me. > >This code, using Arrays, works as I would expect. > >a = [1,2,3] >b = a.dup >a.reverse! >a # -> [3, 2, 1] >b # -> [1, 2, 3] > >Nothing strange there. But some similar code, using String: > >a = "hello" >b = a.dup >a.reverse! >a # -> "olleh" >b # -> "olleh" > >I would have expected 'b' to be unaffected, since it's supposed to be a >different object than 'a'. So I figured that this had to be something with >how String#dup worked, so I tried String#clone insted. This had the exact >same result. >I guess there is some totally logical reason for this String behaviour, >but I first thought that I was hallucinating when I saw it. :-) >I hope that someone can enlighten me about this. > >/Anders >_____________________________________________________________________ >A n d e r s B e n g t s s o n ndrsbngtssn / yahoo.se >Stockholm, Sweden Ruby 1.4x behaves like what you expected. Ruby 1.6x behaves like what you have above. Why? Dat _________________________________________________________________________ Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.