# Noobie question: saving the values

**URL:** <https://www.ruby-forum.com/t/noobie-question-saving-the-values/115864>\
**Category:** Ruby\
**Created:** [September 29, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/noobie-question-saving-the-values/115864 "2007-09-29T13:28:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chillexistence](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@chillexistence](https://www.ruby-forum.com/u/chillexistence)\
**Post date:** [September 29, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/noobie-question-saving-the-values/115864/1 "2007-09-29T13:28:12Z")

</div>

May you forgive me for this irrelevant thread but nonetheless, I need  
your help.  
I’m new at programming and recently I’ve faced with the problem  
connected with the peculiarity of Ruby. Unlike many other languages, the  
variables in it contain not the objects themselves but only links to  
them, right?  
So can you please explain me why do these two operations very close to  
each other result in different ways, considering the fact that both a  
and b contain the same link?

1. 

a=“13”  
b=a  
a=“12”  
puts b --\> 13

1. 

a=“13”  
b=a  
a[1]=“2”  
puts b --\> 12

To be more exact, in my program I need an array and a  
variable=method(array). But when I call the method, the array itself  
changes as well. How do I save the value of the array and get the  
neccesary value of the variable at the same time?  
P.S. And yes, I sought for solution in a guide but so far haven’t found  
anything.

---

<div class="post-metadata">

**Author:** ![chillexistence](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@chillexistence](https://www.ruby-forum.com/u/chillexistence)\
**Post date:** [September 29, 2007, 1:36pm UTC](https://www.ruby-forum.com/t/noobie-question-saving-the-values/115864/2 "2007-09-29T13:36:03Z")

</div>

Hi –

On Sat, 29 Sep 2007, Druhie A. wrote:

> May you forgive me for this irrelevant thread but nonetheless, I need  
> your help.  
> I’m new at programming and recently I’ve faced with the problem  
> connected with the peculiarity of Ruby. Unlike many other languages, the  
> variables in it contain not the objects themselves but only links to  
> them, right?

“Link” isn’t the right word. Variables contain either the actual value  
of the object, or a reference to the object. It’s not that peculiar,  
actually 🙂

> So can you please explain me why do these two operations very close to  
> each other result in different ways, considering the fact that both a  
> and b contain the same link?
> 
> 1. 
> 
> a=“13”  
> b=a  
> a=“12”

As soon as you re-assign to a variable, you completely cut the  
previous assignment. b is still assigned to “13”, but you’re now using  
a to refer to a completely different object (“12”).

> puts b --\> 13
> 
> 1. 
> 
> a=“13”  
> b=a  
> a[1]=“2”

Here, you’re not assigning a new object to a; rather, you’re changing  
the object that a refers to. Since b refers to the same object, when  
you inspect b you see the change reflected.

> puts b --\> 12
> 
> To be more exact, in my program I need an array and a  
> variable=method(array). But when I call the method, the array itself  
> changes as well. How do I save the value of the array and get the  
> neccesary value of the variable at the same time?  
> P.S. And yes, I sought for solution in a guide but so far haven’t found  
> anything.

You can ‘dup’ the array, either before you call it or in the method:

var = method(array.dup)

Keep in mind, though, that the objects inside the array won’t be  
duped. You’re just creating a new array. So you can add objects to the  
new array (or delete objects from it) without affecting the original  
array.

David

---

<div class="post-metadata">

**Author:** ![chillexistence](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@chillexistence](https://www.ruby-forum.com/u/chillexistence)\
**Post date:** [September 29, 2007, 1:47pm UTC](https://www.ruby-forum.com/t/noobie-question-saving-the-values/115864/3 "2007-09-29T13:47:00Z")

</div>

On 9/29/07, Druhie A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. 
> 
> a=“13”  
> b=a  
> a[1]=“2”  
> puts b → 12

You’re first way makes ‘a’ point at a new string object “12”.

It appears that you are thinking of C strings 🙂 Strings in Ruby are a  
bit  
different. You can access individual characters using the [] operator.  
Indexes are 0 based so when you did a[1] where a was a string, you were  
assigning the number ‘2’ to the second position in the string, hence  
overwriting the ‘3’.

What you were probably intending to do was something like this:

> > a = [“13”]  
> > =\> [“13”]

> > b = a  
> > =\> [“13”]

> > a[1] = 2  
> > =\> 2

> > a  
> > =\> [“13”, 2]

Also note that the reason this change reflected when you printed out ‘b’  
is  
because after the assignment “b=a”, ‘b’ will refer to the variable ‘a’.  
Meaning it does not create a new object but instead becomes a name for  
the  
old so that both ‘a’ and ‘b’ are both names for the same object.

Other ways you can append elements to the end of the array:

> > a \<\< “3”  
> > =\> [“13”, 2, “3”]

> > a.push “4”  
> > =\> [“13”, 2, “3”, “4”]

You can checkout the Array methods available to you here:  
[http://www.ruby-doc.org/core/classes/Array.html](http://www.ruby-doc.org/core/classes/Array.html)

I hope this helps,

~Wayne

---

<div class="post-metadata">

**Author:** ![chillexistence](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@chillexistence](https://www.ruby-forum.com/u/chillexistence)\
**Post date:** [September 29, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/noobie-question-saving-the-values/115864/4 "2007-09-29T14:13:21Z")

</div>

Thanks for your quick help, looks like I’m starting to understand  
something and the program to be done by tomorrow works properly at last  
🙂  
I hope you don’t mind if I come again with a couple of another newbie’s  
questions? 🙂  
Thanks a lot once again.
