# Use of dup to remove references

**URL:** <https://www.ruby-forum.com/t/use-of-dup-to-remove-references/145923>\
**Category:** Ruby\
**Created:** [August 15, 2008, 3:44am UTC](https://www.ruby-forum.com/t/use-of-dup-to-remove-references/145923 "2008-08-15T03:44:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 15, 2008, 3:44am UTC](https://www.ruby-forum.com/t/use-of-dup-to-remove-references/145923/1 "2008-08-15T03:44:39Z")

</div>

Hello

I am new to ruby and looking for a brief explanation on why a code  
sample  
works in an unexpected way.

# Beginning of code

def wipe\_from( sentence )  
sentence\_new = sentence  
while sentence\_new.include? ‘(’  
open = sentence\_new.index( ‘(’ )  
close = sentence\_new.index( ‘)’, open )  
sentence\_new[open…close] = ‘’ if close  
end  
sentence\_new  
end

spoken = “I’m not happy with (nonsense) this moon.”  
test = wipe\_from( spoken )

puts spoken  
puts test

# End of code

I’d expect that the variable spoken would be unchanged because it’s  
sentence\_new that’s being modified. It works as expected if I use dup or  
clone, but why is spoken being modified at all? Shouldn’t it just be  
returning the value of sentence\_new?

Brian

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 15, 2008, 4:12am UTC](https://www.ruby-forum.com/t/use-of-dup-to-remove-references/145923/2 "2008-08-15T04:12:09Z")

</div>

Ruby treats all variables as references to an object.  
ie. if i say:  
sentence = “my string”

you can think of it like this:  
“my string” is stored as data somewhere in memory( let’s say memory slot  
13)  
then will hold the value 13. (the memory location that  
contains it’s data)

so if you say:  
sentence\_new = sentence

then it will assign the value 13 to \<sentence\_new\>

so now both \<sentence\_new\> and refers to whatever is at  
memory slot 13.

so now it doesn’t matter if you say:  
sentence\_new[open…close] = ‘’ OR  
sentence[open…close] = ‘’

they’re both modifying the data at memory slot 13.

Hope that helps clear things up.  
-Patrick

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 15, 2008, 4:24am UTC](https://www.ruby-forum.com/t/use-of-dup-to-remove-references/145923/3 "2008-08-15T04:24:24Z")

</div>

From: Brian R. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# def wipe\_from( sentence )

# sentence\_new = sentence

if in doubt, insert

```
p sentence.object_id
p sentence_new.object_id

```

# while sentence\_new.include? ‘(’

# open = sentence\_new.index( ‘(’ )

# close = sentence\_new.index( ‘)’, open )

# sentence\_new[open…close] = ‘’ if close

# end

# sentence\_new

# end

# spoken = “I’m not happy with (nonsense) this moon.”

# test = wipe\_from( spoken )

# puts spoken

# puts test

if in doubt, insert

```
p spoken.object_id
p test.object_id

```

# # End of code

# 

# I’d expect that the variable spoken would be unchanged because it’s

# sentence\_new that’s being modified. It works as expected if I

variables are just references to objects. when you do

y = “test”  
x = y

x and y now references to the original string object “test”

# use dup or clone, but why is spoken being modified at all?

# Shouldn’t it just be returning the value of sentence\_new?

not ruby. use dup or clone to taste 🙂

anyway, in ruby nothing is impossible. In fact, there are many better  
ways 😉

in your case eg, you can use string#gsub

sample,

irb(main):001:0\> spoken = “I’m not happy with (nonsense) this moon.”  
=\> “I’m not happy with (nonsense) this moon.”

irb(main):002:0\> spoken\_new = spoken.gsub(/(.\*)/,“”)  
=\> “I’m not happy with this moon.”

irb(main):003:0\> spoken\_new  
=\> “I’m not happy with this moon.”

irb(main):004:0\> spoken  
=\> “I’m not happy with (nonsense) this moon.”

hth.  
kind regards -botp

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 15, 2008, 5:16am UTC](https://www.ruby-forum.com/t/use-of-dup-to-remove-references/145923/4 "2008-08-15T05:16:08Z")

</div>

Mmm, there’s only two types of operations in Ruby. Assignment, and  
method calls.

sentence = “variable” is an assignment. The object\_id of “variable” is  
put inside a variable named

sentence[3] = ‘t’ is a method call. The method’s name is “[]=”, so  
sentence does whatever the method is defined as:

sentence[3] = ‘t’ does the same thing as:  
sentence.[]=(‘t’)

Assignment ALWAYS assigns a new object.  
Method calls ALWAYS operates directly on the receiving object.

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 15, 2008, 4:41am UTC](https://www.ruby-forum.com/t/use-of-dup-to-remove-references/145923/5 "2008-08-15T04:41:57Z")

</div>

On Thu, Aug 14, 2008 at 10:20 PM, botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> variables are just references to objects. when you do
> 
> y = “test”  
> x = y
> 
> x and y now references to the original string object “test”

Thanks so much for the help. What has been confusing is that the object  
changes in this next bit of test code so that both variables reference  
different objects:

# Start of code that changes object

sentence = “first”  
p sentence.object\_id  
sentence\_new = sentence  
p sentence\_new.object\_id  
sentence = “second”  
p sentence.object\_id  
p sentence\_new.object\_id

# End of code

and I think that I figured out that the [] method doesn’t change the  
object  
referenced, but just the value of the object, while using = to assign a  
new  
value assigns a new object as well. Is that correct? Is there a way to  
use =  
without changing the object in the example above?

# Start of code that preserves object

sentence = “test”  
p sentence.object\_id  
sentence\_new = sentence  
p sentence\_new.object\_id  
sentence[‘t’] = ‘p’  
p sentence.object\_id  
puts sentence\_new  
puts sentence

# End of code

Thanks again  
Brian

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 15, 2008, 8:59am UTC](https://www.ruby-forum.com/t/use-of-dup-to-remove-references/145923/6 "2008-08-15T08:59:18Z")

</div>

From: Brian R. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# def wipe\_from( sentence )

# sentence\_new = sentence

if in doubt, insert

```
p sentence.object_id
p sentence_new.object_id

```

# while sentence\_new.include? ‘(’

# open = sentence\_new.index( ‘(’ )

# close = sentence\_new.index( ‘)’, open )

# sentence\_new[open…close] = ‘’ if close

# end

# sentence\_new

# end

# spoken = “I’m not happy with (nonsense) this moon.”

# test = wipe\_from( spoken )

# puts spoken

# puts test

if in doubt, insert

```
p spoken.object_id
p test.object_id

```

# # End of code

# 

# I’d expect that the variable spoken would be unchanged because it’s

# sentence\_new that’s being modified. It works as expected if I

variables are just references to objects. when you do

y = “test”  
x = y

x and y now references to the original string object “test”

# use dup or clone, but why is spoken being modified at all?

# Shouldn’t it just be returning the value of sentence\_new?

not ruby. use dup or clone to taste 🙂

anyway, in ruby nothing is impossible. In fact, there are many better  
ways 😉

in your case eg, you can use string#gsub

sample,

irb(main):001:0\> spoken = “I’m not happy with (nonsense) this moon.”  
=\> “I’m not happy with (nonsense) this moon.”

irb(main):002:0\> spoken\_new = spoken.gsub(/(.\*)/,“”)  
=\> “I’m not happy with this moon.”

irb(main):003:0\> spoken\_new  
=\> “I’m not happy with this moon.”

irb(main):004:0\> spoken  
=\> “I’m not happy with (nonsense) this moon.”

hth.  
kind regards -botp
