[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

skanemupp

5/9/2008 12:11:00 AM

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?
4 Answers

Matthew Moss

5/9/2008 12:27:00 AM

0



On May 8, 7:15 pm, globalrev <skanem...@yahoo.se> wrote:
> 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?

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

Rick DeNatale

5/9/2008 12:49:00 PM

0

On Thu, May 8, 2008 at 8:15 PM, globalrev <skanemupp@yahoo.se> wrote:
> 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?

http://talklikeaduck.denh...articles/2006/09/13/on-variables-values-a...
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Ron Fox

5/9/2008 2:42:00 PM

0

globalrev wrote:
> 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.
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.

Marc Heiler

5/10/2008 12:12:00 PM

0

> how do i do what i want to do?

I believe you might want to use .dup
--
Posted via http://www.ruby-....