# Principle of le,err i mean biggest surprise!?

**URL:** <https://www.ruby-forum.com/t/principle-of-le-err-i-mean-biggest-surprise/137116>\
**Category:** Ruby\
**Created:** [May 9, 2008, 2:17am UTC](https://www.ruby-forum.com/t/principle-of-le-err-i-mean-biggest-surprise/137116 "2008-05-09T02:17:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![globalrev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@globalrev](https://www.ruby-forum.com/u/globalrev)\
**Post date:** [May 9, 2008, 2:17am UTC](https://www.ruby-forum.com/t/principle-of-le-err-i-mean-biggest-surprise/137116/1 "2008-05-09T02:17:15Z")

</div>

puts “Input sentence:”  
sentence = gets.split()  
puts

temp = sentence  
phrase = sentence  
puts “:”, temp, sentence, phrase

phrase[0] = temp[1]  
puts “oink”, phrase, temp

when changing phrase[0] this will change temp as well and even  
sentence! makes no sense and very surprising. how do i do what i want  
to do?

so basically ami messing with pointers here or what?

---

<div class="post-metadata">

**Author:** ![globalrev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@globalrev](https://www.ruby-forum.com/u/globalrev)\
**Post date:** [May 9, 2008, 2:28am UTC](https://www.ruby-forum.com/t/principle-of-le-err-i-mean-biggest-surprise/137116/2 "2008-05-09T02:28:21Z")

</div>

On May 8, 7:15 pm, globalrev [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> when changing phrase[0] this will change temp as well and even  
> sentence! makes no sense and very surprising. how do i do what i want  
> to do?
> 
> so basically ami messing with pointers here or what?

Essentially, yes, though I believe they are more commonly called  
references (and not in that C++ sort of way).

When you do the assignment, phrase[0] = temp[1], you are simply making  
the 0th item of the phrase array refer to the same object that the 1st  
item of temp refers.

You need to throw in a `dup` call (duplicate) where appropriate. If  
you want temp and phrase to be distinct from sentence, then change  
those assignments to:

```
temp = sentence.dup
phrase = sentence.dup

```

If you wanted only phrase[0] to be distinct from temp[1], then:

```
phrase[0] = temp[1].dup

```

---

<div class="post-metadata">

**Author:** ![globalrev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@globalrev](https://www.ruby-forum.com/u/globalrev)\
**Post date:** [May 9, 2008, 2:49pm UTC](https://www.ruby-forum.com/t/principle-of-le-err-i-mean-biggest-surprise/137116/3 "2008-05-09T14:49:18Z")

</div>

On Thu, May 8, 2008 at 8:15 PM, globalrev [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> when changing phrase[0] this will change temp as well and even  
> sentence! makes no sense and very surprising. how do i do what i want  
> to do?
> 
> so basically ami messing with pointers here or what?

## [http://talklikeaduck.denhaven2.com/articles/2006/09/13/on-variables-values-and-objects](http://talklikeaduck.denhaven2.com/articles/2006/09/13/on-variables-values-and-objects)

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![globalrev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@globalrev](https://www.ruby-forum.com/u/globalrev)\
**Post date:** [May 10, 2008, 9:48am UTC](https://www.ruby-forum.com/t/principle-of-le-err-i-mean-biggest-surprise/137116/4 "2008-05-10T09:48:25Z")

</div>

globalrev wrote:

> when changing phrase[0] this will change temp as well and even  
> sentence! makes no sense and very surprising.  
> Well actually it makes perfect sense… given what a Ruby variable  
> actually is. See below.

how do i do what i want

> to do?
> 
> so basically ami messing with pointers here or what?

Yes you are. Variables in ruby are references to their underlying  
objects. Assignment assigns the references. Use the dup method to  
create copies.

Ron.

---

<div class="post-metadata">

**Author:** ![globalrev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@globalrev](https://www.ruby-forum.com/u/globalrev)\
**Post date:** [May 10, 2008, 2:12pm UTC](https://www.ruby-forum.com/t/principle-of-le-err-i-mean-biggest-surprise/137116/5 "2008-05-10T14:12:22Z")

</div>

> how do i do what i want to do?

I believe you might want to use .dup
