G
Glenn
Hi,=0A=0ACan anyone explain why hash_of_indexes1 in the code below works, b=
ut hash_of_index2 doesn't?=A0 (I got the first method as a response to a pr=
evious post.)=0A=0AWhat I'd like to get is a hash with a key value for each=
unique value in the receiver array, and I'd like the value for each key to=
be an array of integers corresponding to the index values of the elements =
in the original array.=A0 The first method works, but the second one doesn'=
t.=A0 I figured if I created the hash in the second method with a default v=
alue of an empty array, h[e.to_f] in the each method of the hash would retu=
rn=A0an empty array the first time it sees each key.=A0 But what=A0actually=
=A0happens is that the values for all of the keys are the same when the has=
h's each method finishes --=A0each value is=A0an array with all of the inde=
x values in the receiver array.=0A=0Aclass Array=0A=A0 def hash_of_indexes1=
=0A=A0=A0=A0 h =3D Hash.new=0A=A0=A0=A0 each_with_index { |e, i| h[e.to_f] =
=3D Array( h[e.to_f] ) << i }=0A=A0=A0=A0 h=0A=A0 end=0A=0A=A0 def hash_of_=
indexes2=0A=A0=A0=A0 h =3D Hash.new([])=0A=A0=A0=A0 each_with_index { |e, i=
| h[e.to_f] =3D h[e.to_f] << i }=0A=A0=A0=A0 h=0A=A0 end=0Aend=0A=0A[1, 2, =
2, 3].hash_of_indexes1.inspect ## --> returns {1.0=3D>[0], 3.0=3D>[3], 2.0=
=3D>[1, 2]}=0A=0A[1, 2, 2, 3].hash_of_indexes2.inspect ## --> returns {1.0=
=3D>[0, 1, 2, 3], 3.0=3D>[0, 1, 2, 3], 2.0=3D>[0, 1, 2, 3]}=0A=0AThanks for=
your help.=0A=0A=0A=0A=0A=0A----- Original Message ----=0AFrom: William Ja=
mes <[email protected]>=0ATo: ruby-talk ML <[email protected]>=0ASe=
nt: Sunday, September 14, 2008 4:02:54 PM=0ASubject: Re: Passing a method c=
all into a method=0A=0AOn Sep 14, 8:18 am, Glenn <[email protected]> wro=
te:=0A> [Note:=A0 parts of this message were removed to make it a legal pos=
t.]=0A>=0A> Hi,=0A>=0A> I am trying to figure out how to pass a method call=
into a method.=0A>=0A> I wrote a generic method that call be called on an =
array of numbers, and returns an array of the index values in the receiver =
that meet a specified condition:=0A>=0A> class Array=0A>=A0 def hash_of_ind=
exes=0A>=A0 =A0 if self.empty?=0A>=A0 =A0 =A0 'The array is empty.'=0A>=A0 =
=A0 else=0A>=A0 =A0 =A0 h =3D {}=0A>=A0 =A0 =A0 self.each_with_index { |e, =
i| h.include?(e.to_f) ? h[e.to_f] << i : h[e.to_f] =3D }=0A>=A0 =A0 =A0=
h=0A>=A0 =A0 end=0A>=A0 end=0A=0A=0Aclass Array=0A=A0 def hash_of_indexes=
=0A=A0 =A0 h =3D {}=0A=A0 =A0 each_with_index{ |e,i|=0A=A0 =A0 =A0 e =3D e.=
to_f=0A=A0 =A0 =A0 h[e] =3D Array( h[e] ) << i }=0A=A0 =A0 h=0A=A0 end=0Aen=
d
ut hash_of_index2 doesn't?=A0 (I got the first method as a response to a pr=
evious post.)=0A=0AWhat I'd like to get is a hash with a key value for each=
unique value in the receiver array, and I'd like the value for each key to=
be an array of integers corresponding to the index values of the elements =
in the original array.=A0 The first method works, but the second one doesn'=
t.=A0 I figured if I created the hash in the second method with a default v=
alue of an empty array, h[e.to_f] in the each method of the hash would retu=
rn=A0an empty array the first time it sees each key.=A0 But what=A0actually=
=A0happens is that the values for all of the keys are the same when the has=
h's each method finishes --=A0each value is=A0an array with all of the inde=
x values in the receiver array.=0A=0Aclass Array=0A=A0 def hash_of_indexes1=
=0A=A0=A0=A0 h =3D Hash.new=0A=A0=A0=A0 each_with_index { |e, i| h[e.to_f] =
=3D Array( h[e.to_f] ) << i }=0A=A0=A0=A0 h=0A=A0 end=0A=0A=A0 def hash_of_=
indexes2=0A=A0=A0=A0 h =3D Hash.new([])=0A=A0=A0=A0 each_with_index { |e, i=
| h[e.to_f] =3D h[e.to_f] << i }=0A=A0=A0=A0 h=0A=A0 end=0Aend=0A=0A[1, 2, =
2, 3].hash_of_indexes1.inspect ## --> returns {1.0=3D>[0], 3.0=3D>[3], 2.0=
=3D>[1, 2]}=0A=0A[1, 2, 2, 3].hash_of_indexes2.inspect ## --> returns {1.0=
=3D>[0, 1, 2, 3], 3.0=3D>[0, 1, 2, 3], 2.0=3D>[0, 1, 2, 3]}=0A=0AThanks for=
your help.=0A=0A=0A=0A=0A=0A----- Original Message ----=0AFrom: William Ja=
mes <[email protected]>=0ATo: ruby-talk ML <[email protected]>=0ASe=
nt: Sunday, September 14, 2008 4:02:54 PM=0ASubject: Re: Passing a method c=
all into a method=0A=0AOn Sep 14, 8:18 am, Glenn <[email protected]> wro=
te:=0A> [Note:=A0 parts of this message were removed to make it a legal pos=
t.]=0A>=0A> Hi,=0A>=0A> I am trying to figure out how to pass a method call=
into a method.=0A>=0A> I wrote a generic method that call be called on an =
array of numbers, and returns an array of the index values in the receiver =
that meet a specified condition:=0A>=0A> class Array=0A>=A0 def hash_of_ind=
exes=0A>=A0 =A0 if self.empty?=0A>=A0 =A0 =A0 'The array is empty.'=0A>=A0 =
=A0 else=0A>=A0 =A0 =A0 h =3D {}=0A>=A0 =A0 =A0 self.each_with_index { |e, =
i| h.include?(e.to_f) ? h[e.to_f] << i : h[e.to_f] =3D }=0A>=A0 =A0 =A0=
h=0A>=A0 =A0 end=0A>=A0 end=0A=0A=0Aclass Array=0A=A0 def hash_of_indexes=
=0A=A0 =A0 h =3D {}=0A=A0 =A0 each_with_index{ |e,i|=0A=A0 =A0 =A0 e =3D e.=
to_f=0A=A0 =A0 =A0 h[e] =3D Array( h[e] ) << i }=0A=A0 =A0 h=0A=A0 end=0Aen=
d