Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > A little bit confused about array+array addition (bug or expected behavior?)

Reply
Thread Tools

A little bit confused about array+array addition (bug or expected behavior?)

 
 
gga
Guest
Posts: n/a
 
      02-16-2005
irb> a = [1,2]
=> [1, 2]
irb> a
=> [1, 2, 3, 4]
irb(main):082:0> a
=> [1, 2, 3, 4]

So far so good...


irb(main):082:0> def add(x)
irb(main):084:1> x += [5,6]
irb(main):085:1> end

irb(main):087:0> add(a)
=> [1, 2, 3, 4, 5, 6]
irb(main):088:0> a
=> [1, 2, 3, 4] ===>>> uh? am I not passing stuff by reference?
where is 5, 6?


irb(main):089:0> def add2(x)
irb(main):089:0> x << [5,6]
irb(main):089:0> end

irb(main):090:0> add2(a)
=> [1, 2, 3, 4, [5, 6]]
irb(main):091:0> a
=> [1, 2, 3, 4, [5, 6]] ==> seems logical, but then .... why +=
behaves differently?

> ruby -v

ruby 1.8.2 (2004-12-25) [i686-linux]

 
Reply With Quote
 
 
 
 
Florian Gross
Guest
Posts: n/a
 
      02-16-2005
gga wrote:

> irb(main):082:0> def add(x)
> irb(main):084:1> x += [5,6]
> irb(main):085:1> end
>
> irb(main):087:0> add(a)
> => [1, 2, 3, 4, 5, 6]
> irb(main):088:0> a
> => [1, 2, 3, 4] ===>>> uh? am I not passing stuff by reference?
> where is 5, 6?


You are not passing variables by reference, you are passing objects by
reference.

Ruby does not have a built-in way of doing variable by reference as
variables are usually not first-class objects. (They are names for objects.)

You probably want to do x.replace(x + [5, 6]) or x.push(5, 6) which
modify the object itself.

This has come up frequently in the past -- if you are willing to do a
bit of research on google groups you will find implementations of
first-class variable objects, using lambdas for modifying variables in
other scopes and more.
 
Reply With Quote
 
 
 
 
Assaph Mehr
Guest
Posts: n/a
 
      02-16-2005

gga wrote:
>
> irb(main):082:0> def add(x)
> irb(main):084:1> x += [5,6]
> irb(main):085:1> end
>
> irb(main):087:0> add(a)
> => [1, 2, 3, 4, 5, 6]
> irb(main):088:0> a
> => [1, 2, 3, 4] ===>>> uh? am I not passing stuff by

reference?
> where is 5, 6?


In the method body you are reassing to the local variable x.
.. x += [...]
is translated to:
.. x = x + [...]
and #+ as we all know returns a copy of the object.

You can see this with:

.. def add(x)
.. puts "Before #{x.object_id}"
.. x += [5,6]
.. puts "After #{x.object_id}"
.. end

So you ARE passing by ref, but then changing the ref to point to
anotehr object.

> irb(main):089:0> def add2(x)
> irb(main):089:0> x << [5,6]
> irb(main):089:0> end
>
> irb(main):090:0> add2(a)
> => [1, 2, 3, 4, [5, 6]]
> irb(main):091:0> a
> => [1, 2, 3, 4, [5, 6]] ==> seems logical, but then .... why +=
> behaves differently?


The METHOD #<< modifies the object in place. The SYNTACTIC SUGAR += is
changing a objeced referred to.

HTH,
Assaph

 
Reply With Quote
 
gga
Guest
Posts: n/a
 
      02-17-2005
Yes, that's what I had figured. I had not realized the existance of
the replace method. That's exactly what I was looking for.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-17-2005

"Assaph Mehr" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
>
> gga wrote:
> >
> > irb(main):082:0> def add(x)
> > irb(main):084:1> x += [5,6]
> > irb(main):085:1> end
> >
> > irb(main):087:0> add(a)
> > => [1, 2, 3, 4, 5, 6]
> > irb(main):088:0> a
> > => [1, 2, 3, 4] ===>>> uh? am I not passing stuff by

> reference?
> > where is 5, 6?

>
> In the method body you are reassing to the local variable x.
> . x += [...]
> is translated to:
> . x = x + [...]
> and #+ as we all know returns a copy of the object.
>
> You can see this with:
>
> . def add(x)
> . puts "Before #{x.object_id}"
> . x += [5,6]
> . puts "After #{x.object_id}"
> . end
>
> So you ARE passing by ref, but then changing the ref to point to
> anotehr object.
>
> > irb(main):089:0> def add2(x)
> > irb(main):089:0> x << [5,6]
> > irb(main):089:0> end
> >
> > irb(main):090:0> add2(a)
> > => [1, 2, 3, 4, [5, 6]]
> > irb(main):091:0> a
> > => [1, 2, 3, 4, [5, 6]] ==> seems logical, but then .... why +=
> > behaves differently?

>
> The METHOD #<< modifies the object in place. The SYNTACTIC SUGAR += is
> changing a objeced referred to.


Alternative is #concat

>> a=[1,2]

=> [1, 2]
>> def add3(a) a.concat [3,4] end

=> nil
>> add3 a

=> [1, 2, 3, 4]
>> a

=> [1, 2, 3, 4]


Regards

robert

 
Reply With Quote
 
YANAGAWA Kazuhisa
Guest
Posts: n/a
 
      02-17-2005
In Message-Id: <(E-Mail Removed) .com>
"gga" <(E-Mail Removed)> writes:

> Yes, that's what I had figured. I had not realized the existance of
> the replace method. That's exactly what I was looking for.


But what you need in this particular case is Array#concat.

irb(main):001:0> a=[1,2,3]
=> [1, 2, 3]
irb(main):002:0> a.concat([4,5])
=> [1, 2, 3, 4, 5]
irb(main):003:0> a
=> [1, 2, 3, 4, 5]


--
http://www.velocityreviews.com/forums/(E-Mail Removed) February 17, 2005
Time and tide wait for no man.



 
Reply With Quote
 
Stefan Lang
Guest
Posts: n/a
 
      02-17-2005
gga wrote:

> irb(main):082:0> def add(x)
> irb(main):084:1>¬*¬*¬*¬*x¬*+=¬*[5,6]


This creates a new array and assigns it to the local variable x.
The original array passed into this method won't be modified.
But the result of the statement is the new array, which will
be returned.

> irb(main):085:1> end
>
> irb(main):087:0> add(a)
> => [1, 2, 3, 4, 5, 6]
> irb(main):088:0> a
> => [1, 2, 3, 4]¬*¬*¬*¬*¬*¬*¬*¬*===>>>¬*uh?¬*¬*am¬*I¬*not¬*passing¬*stuff¬*by¬*reference?
> where¬*is¬*5,¬*6?
>
>
> irb(main):089:0> def add2(x)
> irb(main):089:0>¬*¬*¬*x¬*<<¬*[5,6]


<< appends to the existing array

> irb(main):089:0> end


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
different materials? - little bit confused Detlef K. MCSA 2 06-05-2008 06:45 AM
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
How to use this class? A little bit confused. Thank You. shapper ASP .Net 0 03-11-2007 04:54 PM
Data ... need advice on this ... a little bit confused. shapper ASP .Net 1 03-08-2007 09:44 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments