Noobie question: saving the values

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?

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

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.

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 :slight_smile:

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?

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

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

On 9/29/07, Druhie A. [email protected] wrote:

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 :slight_smile: 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

I hope this helps,

~Wayne

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
:slight_smile:
I hope you don’t mind if I come again with a couple of another newbie’s
questions? :slight_smile:
Thanks a lot once again.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs