From: Eric Hodel Date: 2005-02-05T08:05:58+09:00 Subject: Re: iteration the ruby way --Apple-Mail-48-402084306 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed On 04 Feb 2005, at 14:18, Navindra Umanee wrote: > John Wilger wrote: >>> I have a question about The Ruby Way. Pickaxe gives the following >>> example for using iterators in Ruby: >>> >>> a = [ "a", "b", "c" ] >>> a.each {|x| print x, " -- " } >>> >>> This outputs: >>> >>> a -- b -- c -- >>> >>> But what if I want to print "a -- b -- c"? What's the proper Ruby >>> way >>> of doing that? >> >> a = ["a","b","c"] >> puts a.join(' - ') > > I guess I over-simplified. I really want to do computations based on > each element and compute my output. However, it's slightly different > for the last element. I would like to detect the last element in the > iteration. > > For example: > > bottom_items.each{ |item| > do_something_with(item[0], item[1], item[2]) > if_not_last_item_do_this > } items = items.map do |item| do_something_with item end puts items.join(' -- ') -- Eric Hodel - drbrain@segment7.net - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --Apple-Mail-48-402084306 content-type: application/pgp-signature; x-mac-type=70674453; name=PGP.sig content-description: This is a digitally signed message part content-disposition: inline; filename=PGP.sig content-transfer-encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFCA/+fMypVHHlsnwQRAu+xAJwIRqHdvUzGOyZ//DtJLmEDbQ53uACgvQuP nbNiSOMoVetEzGql2WIIji4= =zd2M -----END PGP SIGNATURE----- --Apple-Mail-48-402084306--