# Just started learning Ruby

**URL:** <https://www.ruby-forum.com/t/just-started-learning-ruby/86710>\
**Category:** Ruby\
**Created:** [February 24, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/just-started-learning-ruby/86710 "2007-02-24T22:11:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DracoJK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac91a4/32.png) [@DracoJK](https://www.ruby-forum.com/u/DracoJK)\
**Post date:** [February 24, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/just-started-learning-ruby/86710/1 "2007-02-24T22:11:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DracoJK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac91a4/32.png) [@DracoJK](https://www.ruby-forum.com/u/DracoJK)\
**Post date:** [February 24, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/just-started-learning-ruby/86710/2 "2007-02-24T22:20:31Z")

</div>

On 2/24/07, DracoJK [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> second\_var.chop! # Chops off the last character of the string  
> 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. 😛

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

---

<div class="post-metadata">

**Author:** ![DracoJK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac91a4/32.png) [@DracoJK](https://www.ruby-forum.com/u/DracoJK)\
**Post date:** [February 24, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/just-started-learning-ruby/86710/3 "2007-02-24T22:26:31Z")

</div>

Alle sabato 24 febbraio 2007, DracoJK ha scritto:

> second\_var.chop! # Chops off the last character of the string  
> 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. 😛

In both cases, writing second\_var=first\_var makes the two variables  
contain  
the same object (you can see this by using the object\_id method). The  
chop!  
method changes the contents of its receiver, that is of the string  
contained  
by both first\_var and second\_var. On the other hand, in your second  
example,  
when you write second\_var+=1, which is translated to  
second\_var=second\_var+1,  
you’re storing a different value in second\_var, not changing the object  
it  
contains.

Note that the difference isn’t due to the fact that in the first case  
you’re  
using strings and in the second case you’re using integers (although  
integers  
have some peculiarities, in particular, there’s only one integer object  
for  
each number). If you do something like:

first\_var=“a string”  
second\_var=first\_var #now first\_var and second\_var contain the same  
object  
second\_var=second\_var.chop #chop doesn’t modify the original string  
puts second\_var  
=\> a strin  
puts first\_var  
=\> a string

you get the same behaviour you got with integers.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![DracoJK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac91a4/32.png) [@DracoJK](https://www.ruby-forum.com/u/DracoJK)\
**Post date:** [February 25, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/just-started-learning-ruby/86710/4 "2007-02-25T12:01:26Z")

</div>

It’s all about “immediate values”. See:

[http://www.rubycentral.com/faq/rubyfaq-9.html#ss9.3](http://www.rubycentral.com/faq/rubyfaq-9.html#ss9.3)

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)
