From: Wyatt Greene Date: 2008-05-04T22:45:03+09:00 Subject: Re: Different Ways To Loop On May 4, 9:34 am, "David A. Black" wrote: > Hi -- > > > > On Sun, 4 May 2008, Ashley Wharton wrote: > > On Sun, May 4, 2008 at 8:55 AM, Wyatt Greene wrote: > > >> On May 4, 1:54 am, Mike McKinney wrote: > >>> [Note: parts of this message were removed to make it a legal post.] > > >>> of course there's... > > >>> i=0 and while(i<100) do > >>> puts i+=1 > >>> end > > >>> i=0 and until(i==100) do > >>> puts i+=1 > >>> end > > >> > On Sat, May 3, 2008 at 11:48 PM, Dan Zwell wrote: > >>>> Don't forget the "traditional" style loop: > > >>>> for x in 1..100 do > >>>> puts x+1 > >>>> end > > >>>> ara.t.howard wrote: > > >>>>> On May 3, 2008, at 8:10 PM, Wyatt Greene wrote: > > >>>>>> 1.upto(100) { |x| puts x } > > >>>>> here are a few > > >>>>> # 1 > >>>>> puts Array.new(100){|i| i + 1} > > >>>>> # 2 > >>>>> i = 0 and loop do > >>>>> puts i+=1 > >>>>> break if i >= 100 > >>>>> end > > >>>>> # 3 > >>>>> puts <<-chars.strip.split(%r//).map{|c| c[0]} > > >> \001\002\003\004\005\006\a\b\t\n\v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\e\034\035\036\037!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd > > >>>>> chars > > >>>>> # 4 > >>>>> 100.times{|i| puts i + 1} > > >>>>> a @http://codeforpeople.com/ > >>>>> -- > >>>>> we can deny everything, except that we have the possibility of being > >>>>> better. simply reflect on that. > >>>>> h.h. the 14th dalai lama > > >>> -- > >>> Aloha! > > >>> Mike McKinney > >>> m...@huikau.com > >>> (http://blog.huikau.com) > > >> And here's an "object-oriented" way: > > >> class Counter > >> def count > >> @count ||= 1 > >> puts @count > >> @count += 1 > >> @count > 100 > >> end > >> end > > >> counter = Counter.new > >> until counter.count; end > > >> Based on a response from Eric when I first asked for alternative ways to > > do this: > > > MAX = 100 > > def print_from_to(start, stop) > > start.upto(stop) do |number| > > puts number > > end > > end > > loop do > > number = 1 > > print_from_to(number, MAX) > > break > > end > > I can think of lots of ways: > > puts *1..100 > puts *1..101-1 > puts *1..102-2 ... > > or > > "This evaluates to true" and loop do ... > > "So does this" and loop do ... > > :-) > > I don't mean to scoff at the "more than one way" thing, but it does > in a sense contain the seeds of its own destruction :-) > > David > > -- > Rails training from David A. Black and Ruby Power and Light: > INTRO TO RAILS June 9-12 Berlin > ADVANCING WITH RAILS June 16-19 Berlin > INTRO TO RAILS June 24-27 London (Skills Matter) > Seehttp://www.rubypal.comfor details and updates! This discussion will self-destruct after 20 posts... :)