From: "Shot (Piotr Szotkowski)" Date: 2009-06-12T03:25:56+09:00 Subject: Re: Idiomatic way to detect first/last iteration? --OXfL5xGRrasGEqWY Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Kendall Gifford: > Hi there, is there any recognized ruby idiomatic way for detecting > when you're on the first and/or last iteration over a collection? [=E2=80= =A6] > genres =3D %w{ classical punk rock jazz } > genres.each_with_index do |genre, i| > desc =3D "one of my favorites" > desc =3D "my absolute favorite" if i.zero? > desc =3D "my least fav. of all my favorites" if i + 1 =3D=3D genres.len= gth > puts "#{genre} is #{desc}" > end In general, #equal? should check object identity: genres =3D %w{ classical punk rock jazz } genres.each do |genre| case when genre.equal?(genres.first) then puts "#{genre} is my absolute favori= te" when genre.equal?(genres.last) then puts "#{genre} is my least fav. of a= ll my favorites" else puts "#{genre} is one of my favorite= s" end end =E2=80=94 Shot --=20 Wife likes =E2=80=98Go Diego Go=E2=80=99 because =E2=80=98it teaches the bo= ys Spanish=E2=80=99. That shit teaches you Spanish like porn teaches you social skills. [Mark Pilgrim] --OXfL5xGRrasGEqWY Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkoxTDAACgkQi/mCfdEo8UoxqACcDfSdbLSKbykIsflZE2AK+gt6 1IIAoJB5ZCCY76xwDjYGqvS8BMLRHS36 =YDDs -----END PGP SIGNATURE----- --OXfL5xGRrasGEqWY--