[Rake-devel] task - block vs. to_proc

Turpin, Jay jay.turpin at intel.com
Tue Jun 12 00:45:07 EDT 2007


I am probably missing something obvious, but I don't understand why this
won't work. I have created a BaseTask class that I use to simplify the
creation of custom Rake tasks. I'd like to let the developer create a
new task like this:

class SampleTask < BaseTask
  attr_accessor :name, :directory
    
  def initialize(params)
    super params
  end

  setup_task
    logger.info "Running Task #{task_name}"
    puts "Name: #{name}"
    puts "directory: #{directory}"
  end
end

and then convert the method to a Proc in the base class and assign it to
the task:

class BaseTask < Rake::TaskLib
  attr_accessor :task_name
		
  def initialize(params=:my_task_name)
    @task_name = case params
      when Hash
        task params.keys[0] => params[params.keys[0]]
        params.keys[0]
      else
        params
      end

      yield self if block_given?
      create_task
  end
    	
  def create_task
    task task_name, method(:setup_task).to_proc
    self
  end
end

It doesn't work. I get a wrong number of parameters error. However, if I
define setup_task() as a method that accepts a block, I can save that
block in a variable and then assign it. Something like this:

  setup_task(&block)
    @setup_task = block
  end

  def create_task
    task task_name, &@setup_task
    self
  end

During inspection, they both resolve to Proc objects, so I am confused.
Any help would be appreciated. Thanks!

Regards,
Jay Turpin
"Respect cannot be learned, purchased, or acquired; it can only be
earned." - Bits & Pieces

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rake-devel/attachments/20070611/d4877993/attachment-0001.html 


More information about the Rake-devel mailing list