From: AW Date: 2002-08-22T04:53:29+09:00 Subject: Re: the power of ruby Your version runs about 10X faster than mine. but i see why now. I had far to many writes to IO. The precedence is a bit confusing at first. I wasnt aware that the times{} must complete for going to downto{}. this is a wonderfull example. I think I may be a bit too timid about truely exploiting Ruby's power. > >> I keep looking at the last line and I cant seem to grasp its >> relationship, I mean, If the first set of '{}' are containing the loop >> from the '.times' method, how do the second set relate? > >The .times method returns an int, try this : > >C:\> ruby -e "puts 5.times{}" >5 >C:\> > >The block in curly brace: { |i| do_dot(i) } is only a parameter to .times. >Imagine it thus: > >WIDTH.times( block_in_curly_brace) > >Since the return value of this function is an int you can cascade it to get > >WIDTH.times( block_in_curly_brace).downto(0) { |i| do_dot(i) } > >> Now i'm really confused. Thank you! ;-) > >Hope I have not confused you more ;-) > >-- Shanko > >> >> >Yours is rather succinct, but I find that you can often capitalize >> >on return values of methods to shorten. Shorten, right? You're >> >looking for shorter? >> >> Im looking for exactly what you gave. different ways to express the >> same output in Ruby. Shorter, longer, obsfucated, clear. >> I just hope others will chime in and give some examples of their own >> style. >> >> > >> >Here's one: >> > >> > WIDTH = 78 >> > def do_dot( i ) >> > puts( ( " " * i ) + "*" ) >> > end >> > loop { >> > WIDTH.times { |i| do_dot(i) }.downto(0) { |i| do_dot(i) } >> > } >> > >> >Check ya later. >> > >> >_why >> > >> >AW (sturmpanzer@metacrawler.com) wrote: >> >> >> >> Here is my unimaginitive attempt. >> >> >> >> [code]============================================== >> >> WIDTH=78; >> >> def do_dot(i) >> >> i.times{print " "} >> >> puts "*"; >> >> end >> >> loop{ >> >> 0.upto(WIDTH-1){|i| >> >> do_dot(i); >> >> } >> >> WIDTH.downto(1){|i| >> >> do_dot(i); >> >> } >> >> } >> >> [/code]============================================= >> > >> > >