From: Cliff Rosson Date: 2012-09-21T23:32:10+09:00 Subject: Re: the behavior of iterator methods --14dae934071bcd0e8904ca371aec Content-Type: text/plain; charset=ISO-8859-1 So you can accomplish what you want like so. a = [1, 2, 3] a.each_with_index { |x, index| a[index] += 1 } puts a Or (0...a.length).each { |x| a[x] += 1 } On Wednesday, September 19, 2012, Jam Bees wrote: > Some methods change objects in place, others return a changed object and > leave the original in place. > > The bang is placed by convention at the end of methods that have some > unusual behavior; most often, this unusual behavior is that a data > structure is passed in by reference not by value. > > On Sep 19, 2012, at 9:32 PM, ten ten > > wrote: > > > Hi all, > > > > Sorry, I deleted my previous post on the ruby forum by mistake. > > So I post it again. > > > > > > > > I am a Ruby-beginner. > > I have some experience of C/C++ for several years. > > It has been only one week since I started to learn Ruby. > > Please tell me about the behavior of iterator methods. > > > > At first, I did as bellow on the irb environment. > > ------------------------------------------------------- > > irb(main):001:0> s = ["a", "b", "c"] > > => ["a", "b", "c"] > > irb(main):002:0> s[0].object_id > > => 36971304 > > irb(main):003:0> s[1].object_id > > => 36971292 > > irb(main):004:0> s[2].object_id > > => 36971280 > > irb(main):005:0> s.each{|c| c.upcase!} > > => ["A", "B", "C"] > > irb(main):006:0> p s > > ["A", "B", "C"] > > => nil > > ------------------------------------------------------- > > > > Looking at this, I thought I can change the value of each element in > > Array object through iterator methods. > > > > However, when I did next as bellow, that behavior looked different. > > ------------------------------------------------------- > > irb(main):007:0> a = [1,2,3] > > => [1, 2, 3] > > irb(main):008:0> a[0].object_id > > => 3 > > irb(main):009:0> a[1].object_id > > => 5 > > irb(main):010:0> a[2].object_id > > => 7 > > irb(main):011:0> a.each{|i| i += 1} > > => [1, 2, 3] > > irb(main):012:0> p a > > [1, 2, 3] > > => nil > > > > irb(main):013:0> a.each{|i| p i.object_id} > > 3 > > 5 > > 7 > > => [1, 2, 3] > > ------------------------------------------------------- > > > > I expected that Array#each method with the code block would change > > contents of the Array object variable named 'a' into [2,3,4]. > > But it didn't. Why? > > > > The object IDs of a[0], a[1], a[2] are shown through > > the the code block of Array#each. So I thought if variable named 'i' > > changed its value, that had to be reflected to the Array object 'a'. > > But it didn't. > > > > > > Why did this difference happened? > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > -- http://about.me/cliffrosson vizualize.me/cliffrosson --14dae934071bcd0e8904ca371aec Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable So you can accomplish what you want = like so.

a =3D [1, 2, 3]
a.each_with_index { |x, inde= x| a[index] +=3D 1 }
puts a

Or

(0...a.length).each { |x| a[x] +=3D 1 }

On Wednesday, September 19, 2012, Jam Bees wrote:
Some methods change objects in place, others return a changed object and le= ave the original in place.

=A0The bang is placed by convention at the end of methods that have some un= usual behavior; most often, this unusual behavior is that a data structure = is passed in by reference not by value.

On Sep 19, 2012, at 9:32 PM, ten ten <lists@ruby-f= orum.com> wrote:

> Hi all,
>
> Sorry, I deleted my previous post on the ruby forum by mistake.
> So I post it again.
>
>
>
> I am a Ruby-beginner.
> I have some experience of C/C++ for several years.
> It has been only one week since I started to learn Ruby.
> Please tell me about the behavior of iterator methods.
>
> At first, I did as bellow on the irb environment.
> -------------------------------------------------------
> irb(main):001:0> s =3D ["a", "b", "c"= ]
> =3D> ["a", "b", "c"]
> irb(main):002:0> s[0].object_id
> =3D> 36971304
> irb(main):003:0> s[1].object_id
> =3D> 36971292
> irb(main):004:0> s[2].object_id
> =3D> 36971280
> irb(main):005:0> s.each{|c| c.upcase!}
> =3D> ["A", "B", "C"]
> irb(main):006:0> p s
> ["A", "B", "C"]
> =3D> nil
> -------------------------------------------------------
>
> Looking at this, I thought I can change the value of each element in > Array object through iterator methods.
>
> However, when I did next as bellow, that behavior looked different. > -------------------------------------------------------
> irb(main):007:0> a =3D [1,2,3]
> =3D> [1, 2, 3]
> irb(main):008:0> a[0].object_id
> =3D> 3
> irb(main):009:0> a[1].object_id
> =3D> 5
> irb(main):010:0> a[2].object_id
> =3D> 7
> irb(main):011:0> a.each{|i| i +=3D 1}
> =3D> [1, 2, 3]
> irb(main):012:0> p a
> [1, 2, 3]
> =3D> nil
>
> irb(main):013:0> a.each{|i| p i.object_id}
> 3
> 5
> 7
> =3D> [1, 2, 3]
> -------------------------------------------------------
>
> I expected that Array#each method with the code block would change
> contents of the Array object variable named 'a' into [2,3,4].<= br> > But it didn't. Why?
>
> The object IDs of a[0], a[1], a[2] are shown through
> the the code block of Array#each. So I thought if variable named '= i'
> changed its value, that had to be reflected to the Array object 'a= '.
> But it didn't.
>
>
> Why did this difference happened?
>
> --
> Posted via ht= tp://www.ruby-forum.com/.
>



--
http://about.me/cliffrosson
vizualize.me/cliffrosson
--14dae934071bcd0e8904ca371aec--