Forums | Admin

Discussion Forums: rubys_taqueria_espiritu

Start New Thread Start New Thread
Message: 4745
BY: Leon Barrett (phaedrus)
DATE: 2005-08-17 05:15
SUBJECT: RE: Help Ruby do good Ruby

 

Dangit. I really meant to finish that. Suppose you have a function 'subcase' to easily get your subcases. Then this will work:

def walk(top,&block)
block.call(top)
subcases(top).each { |case| walk(case,&block) }
end

To print all subcases, you can do this:
walk("top"){|top| puts(top)}

Or, to upload them all:
walk("top"){|file| upload(file)}

It's really quite handy. (Note: you can use 'yield' to do the same thing as 'block.call', which means you don't have to include the '&block' bit.) However, in a case like this it's more efficient to keep passing the block. Here's the less efficient version:

def walk(top)
yield(top)
subcases(top).each do |case|
walk(case){|newtop|yield(newtop)
end
end

However, this is less efficient because each subcase is called through a whole chain of blocks.


Thread View

Thread Author Date
Help Ruby do good RubyRuby Dos Zapatas2005-08-02 22:48
      RE: Help Ruby do good RubyLeon Barrett2005-08-17 05:03
            RE: Help Ruby do good RubyLeon Barrett2005-08-17 05:15
                  RE: defining a blockRuby Dos Zapatas2005-08-17 15:19
                        RE: defining a blockLeon Barrett2005-08-17 16:45
                              I found it!Ruby Dos Zapatas2005-08-18 15:32
                                    RE: I found it!Leon Barrett2005-08-18 19:43
                                          You are rightRuby Dos Zapatas2005-08-20 14:12

Post a followup to this message