From: Joel VanderWerf Date: 2006-04-26T14:13:34+09:00 Subject: Re: Rake task dependeny vs. method call ara.t.howard@noaa.gov wrote: > On Wed, 26 Apr 2006, Trans wrote: > >> Just for fun I put together this simple demonstration. > > check out tsort. you need it to work if you have a diamond like > > b => a > c => a > d => b > d => c > > else you'll run 'a' twice. tsort let's you build a dag of dependancies and > even detects cycles. > > cheers. > > > -a He (OP) already did the t-sort, implicitly. Here's a slightly modified version of T's code (#cache happens to be the name of #memoize in my libs). The output is: a b c ---------------------- require 'cache' module Task # Store the dependencies. def task_dependencies @task_dependencies ||= {} end # Store the decriptions. def task_descriptions @task_descriptions ||= {} end # Set the description of the subsequent task. def desc( line ) @last_description = line.gsub("\n",'') end # Define a task. def task( args, &action ) if Hash === args raise ArgumentError, "#{args.size} for 1" if args.size != 1 name, *deps = *(args.to_a.flatten) name = name.to_sym deps = deps.compact.collect{ |e| e.to_sym } else name, deps = args.to_sym, [] end task_dependencies[ name ] = deps task_descriptions[ name ] = @last_description @last_description = nil (class << self; self; end).class_eval do define_method( name ) do deps.each{ |d| send(d) } action.call if action end cache name end end end include Task task :a do puts "a" end task :b => :a do puts "b" end task :c => :a do puts "c" end task :d => [:b, :c] d -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407