ruby - Overwriting #each; can I pass it args through #map, #select, etc? -


I have written my own tree class which contains anonymous tree then returns a # iH function. As a result, it automatically receives numeric functions like #map , #select , #find

Here's the problem when I wrote for #each my tree , I gave a code which is # iH Is an argument used by the name of the tree traversell algorithm, such as : pre_order or : breadth_first . But now when I #map or #inject or #any? et, it can only use the default traversal algorithm, is there any way that I can pass this argument through another innovative function? Here are my criteria;

  • I should be able to use any traversal algorithm for any numerical function. It is very important because different algorithms for trees are very different Can be demonstrated.
  • I do not want to rewrite each single numerical function to pass this argument to #each ; Which defeats the purpose of the module.

    This is a short version of my code;

      are included in the class tree ... # overwrite #hash, and make it algorithmic argument. DRF Each (Algorithm =: Pre_Order, and Block) if TravelWill_Algorithms are included in this? Algorithms self.send (algorithm, and block) other self.method_missing (algorithm) end-end pre-order (& amp; block) sub-self.branches.each Branch; Branch.pre_order (& amp; block) end-end def post_order (& block) ... end def fourth_ & nbsp; (& Amp; Block) ... and end   

    I want to call things like this;

      tree.find (13, breadth_first) tree.any? (: Post_order) do | Node | node.root & gt; = 10 end    

    I'm pretty stupid.

    The method #enum_for gives me all the power here by adding the traditional line

      tree.breadth_first.find (13)   

    Apply the syntax of Charlie

      Change to self.enum_for (__ method__) until block_life is not there?  tree.breadth_first  will return a  calculator   

    in each of my traversal ways that calculates the width according to the algorithm first; Any enforced method will be used internally for that calculation.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -