From: Albert Ng Date: 2007-03-21T05:45:25+09:00 Subject: Re: C(++) For Loop Equivalents ------=_Part_116457_4915324.1174423522121 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Is there any reason you can't use the syntactical sugar "for"? Or maybe construct a range: (i...((maxNum*2)/j) ).each_with_index do |stuff| end On 3/20/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote: > > Pe=F1a wrote: > > From: Michael W. Ryder [mailto:_mwryder@worldnet.att.net] : > > > > # He would need to initialize i before your code as it could be > > # any value, > > # not just 0. By the same token the increment could be any number, not > > # just 1. > > > > fwiw, this is a simple example (intended for nubies only). > > > > C:\family\ruby>cat -n test.rb > > 1 #--------------------------- > > 2 #emulate c++ for (the better) > > 3 #for(int i=3D0; i*j/2 > 4 # { > > 5 # //cool method stuff > > 6 # } > > 7 #--------------------------- > > 8 > > 9 # using normal while loop > > 10 puts "using while..." > > 11 j=3D20 > > 12 i=3D0 > > 13 maxnum=3D100 > > 14 while i*j/2 < maxnum do > > 15 puts i > > 16 i+=3D1 > > 17 end > > 18 > > 19 # let's make our own for() and call it for_simple :) > > 20 # note, there's no condition here > > 21 # we're leaving it up to yield/blocks since that > > 22 # is the ideal way, imho > > 23 def for_simple (init=3D0,incr=3D1) > > 24 i =3D init > > 25 loop do > > 26 yield i > > 27 i +=3D incr > > 28 end > > 29 end > > 30 > > 31 # now let's test it > > 32 puts < > 33 Test I. > > 34 using our own simple for()... note advantage, > > 35 we don't need to track the counter... > > 36 TEXT > > 37 > > 38 for_simple do |i| > > 39 break unless i*j/2 < maxnum > > 40 puts i > > 41 end > > 42 > > 43 puts < > 44 Test II. > > 45 here we initialize to 2 and increment by 2 > > 46 again, we don't need to track the counter... > > 47 TEXT > > 48 > > 49 for_simple(2,2) do |i| > > 50 break unless i*j/2 < maxnum > > 51 puts i > > 52 end > > 53 > > 54 puts < > 55 Test III. > > 56 again, here we initialize to 200 and decrement by 20 > > 57 again, we don't need to track the counter... > > 58 and note, we change keyword unless to if > > 59 ruby just simply rocks.. > > 60 TEXT > > 61 > > 62 for_simple(200,-20) do |i| > > 63 break if i*j/2 < maxnum > > 64 puts i > > 65 end > > > > C:\family\ruby>ruby test.rb > > using while... > > 0 > > 1 > > 2 > > 3 > > 4 > > 5 > > 6 > > 7 > > 8 > > 9 > > Test I. > > using our own simple for()... note advantage, > > we don't need to track the counter... > > 0 > > 1 > > 2 > > 3 > > 4 > > 5 > > 6 > > 7 > > 8 > > 9 > > Test II. > > here we initialize to 2 and increment by 2 > > again, we don't need to track the counter... > > 2 > > 4 > > 6 > > 8 > > Test III. > > again, here we initialize to 200 and decrement by 20 > > again, we don't need to track the counter... > > and note, we change keyword unless to if > > ruby just simply rocks.. > > 200 > > 180 > > 160 > > 140 > > 120 > > 100 > > 80 > > 60 > > 40 > > 20 > > > > C:\family\ruby> > > > > Hth. > > kind regards -botp > > > > > > Now is it possible, like in Scheme, to assign a variable to a string and > have Ruby evaluate the string? For example, f =3D "i*j/2". This would > allow one to make a more generic version of your methods. Ideally you > would have for_opt (i=3D0, f, inc=3D1) and have the method evaluate each = of > the variables if necessary. This would allow one to copy working C code > without having to remember to do a lot of extraneous coding. > > > ------=_Part_116457_4915324.1174423522121--