Mathias Wittlock
2006-Nov-21 23:49 UTC
Destructive string operations working on multiple variables?
Hey all, I guess this should go on the Ruby-list, but I failed to find what it''s called, so here goes. This might be a bit of a stupid question, but I would like to understand why what causes this behaviour: params[''string''] = "foo_bar" @baz = params[''string''] @baz.gsub!(/[_]/, '' '') #replace underscore with space puts params[''string''] # => "foo bar" Why whould gsub!() alter the value in params? Same goes if I copy it again and as far as I can tell even if variables doesn''t have the @-sign or using any other string operator ending with an explamation point. Does my equal sign make references rather then copies? Is there only some occasions when that is true? Thanks in advance, Mathias. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Alex Wayne
2006-Nov-22 07:19 UTC
Re: Destructive string operations working on multiple variab
Mathias Wittlock wrote:> Hey all, > > I guess this should go on the Ruby-list, but I failed to find what > it''s called, so here goes. > > This might be a bit of a stupid question, but I would like to > understand why what causes this behaviour: > > params[''string''] = "foo_bar" > @baz = params[''string''] > @baz.gsub!(/[_]/, '' '') #replace underscore with space > puts params[''string''] # => "foo bar" > > Why whould gsub!() alter the value in params? Same goes if I copy it > again and as far as I can tell even if variables doesn''t have the > @-sign or using any other string operator ending with an explamation > point. > > Does my equal sign make references rather then copies? Is there only > some occasions when that is true? > > Thanks in advance, > Mathias.In ruby, usually, when you assign one variable to another, both variables point to the same value. You can use the object_id method to test this. objects with the same object_id are the same object. > a = ''foo'' > a.object_id #=> 964138 > b = ''foo'' > b.object_id #=> 935788 > b = a > b.object_id #=> 964138 You can usually call the dup method to create a new copy of any object > a = b > a.object_id == b.object_id #=> true > a = b.dup > a.object_id == b.object_id #=> false -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Isak Hansen
2006-Nov-22 12:06 UTC
Re: Destructive string operations working on multiple variables?
On 11/22/06, Mathias Wittlock <wittlock+rubyonrailslist-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hey all, > > I guess this should go on the Ruby-list, but I failed to find what > it''s called, so here goes.IMO the best place for ruby discussions is comp.lang.ruby on usenet. For instance through <http://groups-beta.google.com/group/comp.lang.ruby/topics>.> Does my equal sign make references rather then copies? Is there only > some occasions when that is true?See 4.1 here <http://www.rubycentral.com/faq/rubyfaq.html>. Isak> Thanks in advance, > Mathias. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---