From: Chris Carter Date: 2007-02-25T06:19:44+09:00 Subject: Re: Just started learning Ruby On 2/24/07, DracoJK wrote: > I've been working through the Humble Ruby Book that I found in a signature > of one of ruby talk's list members, and would like a little more explanation > as to how pointing to a pointer works. > > The example used was a string: > > first_var = "i hold a reference" > → i hold a reference > second_var = first_var > → i hold a reference > second_var.chop! # Chops off the last character of the string > → i hold a referenc > first_var > → i hold a referenc > > This works fine... I understand this... but I tried it with an integer > rather than a string, and it doesn't seem to work how I expect it to. > > first_var = 1 > second_var = first_var > second_var += 1 > puts first_var > > this outputs 1, but I thought it'd output 2... o_O > > What's the difference? Why do variables work differently with integers as > opposed to strings? > > Thanks for answering my ever so simple question. :) I come from a java > background and never really thought about how things work when you point a > variable to another variable. :P > Because the += operator actually reassigns the object, so you are doing something like first_var = 1 second_var = first_var hold = second_var + 1 second_var = hold -- Chris Carter concentrationstudios.com brynmawrcs.com