From: why the lucky stiff Date: 2002-08-21T09:46:58+09:00 Subject: Re: the power of ruby AW (sturmpanzer@metacrawler.com) wrote: > Wow, Doing it that way had never accured to me. > How does that work that you can use dot notation from a bracket '}' ? > 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? > Now i'm really confused. Thank you! ;-) The bracketed code is a Proc object and I'm just manipulating its return value directly. I could do this: n = 78.times { |i| do_dot( i ) } n.downto( 0 ) { |i| do_dot( i ) } See, the Integer#times method returns the original Integer. So does Integer#downto. Try these in IRb: 5.times {} ==>5 5.downto( 0 ) {} ==>5 So I can manipulate the return value by calling methods from the block. The above code can be rewritten: 78.times do |i| do_dot( i ) end. downto( 0 ) do |i| do_dot( i ) end I have thought there might be a way to loop up and down through a Range or Integer in Ruby in a single call, but I could think of how to do it without adding a method to Integer or Range and wasting more chars than the above. > 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. Well, in that case, I'll include one that overrides Fixnum for ya: def do_dot( i ) puts "#{' ' * i}*" end class Fixnum def up_and_down( &p ) times( &p ); downto( 0, &p ) end end while 78.up_and_down { |i| do_dot( i ) }; end Good luck. _why