From: "Erik Bågfors" Date: 2001-08-08T02:35:00+09:00 Subject: [ruby-talk:19302] Re: Some remarks from a nembie in Ruby On 06 Aug 2001 22:06:48 +0900, Chris Moline wrote: > On Mon, Aug 06, 2001 at 05:24:47PM +0900, Renaud HEBERT wrote: > > 8) Between > > [ 1, 2, 3, 5 ].each {|val| print val, " " } > > and > > foreach val in ([ 1, 2, 3, 5 ]) {print val, " " } > > I find that the second is much more intuitive: It reads nearly as en > > English sentence. > > The first one is "pushed" quite heavily in the book, but isn't very > > readable IMHO. > > I agree but I have never been able to get foreach to work in my scripts. They > always fail with parse errors. Is it really allowed in ruby?? I would much > prefer foreach to for since it "reads" better. > : [bagfors@detrius]$ ; ruby -e 'a=[1,2,3,4]; for b in a; p b; end' 1 2 3 4 I like each better though. Also remember that with blocks/iterators you can do other stuff like each_second or each_pair and stuff like that. You can't really do that easily with for/foreach... for example [1,2,3,4].each_second do |x| puts x end -> 2,4 With for-loops that would be for x in [1,2,3,4].get_every_second p x end I think the first one is more easy to read and understand. BTW, I made up each_second and get_every_second :) /Erik -- Erik B�gfors | erik@bagfors.nu Supporter of free software | GSM +46 733 279 273 fingerprint: 6666 A85B 95D3 D26B 296B 6C60 4F32 2C0B 693D 6E32