[Rake-devel] task - block vs. to_proc

Turpin, Jay jay.turpin at intel.com
Wed Jun 13 01:46:30 EDT 2007


Assaf,

 

Thanks for the reply. I tried your suggestion, but still got the same
issue. However, I looked at the error more closely and realized what the
problem really is. Rake is calling the block and passing in an instance
of self as a parameter. The method I defined did not have any parameters
defined, so it was throwing an error. As soon as I change the method to
the following, it worked fine:

 

setup_task(task)

  logger.info "Running Task #{task_name}"

  puts "Name: #{name}"

  puts "directory: #{directory}"

end

 

Thanks again

 

Regards, 
Jay Turpin 
"Advice is what we ask for when we already know the answer but wish we
didn't." - Erica Jong 

From: rake-devel-bounces at rubyforge.org
[mailto:rake-devel-bounces at rubyforge.org] On Behalf Of Assaf Arkin
Sent: Monday, June 11, 2007 10:06 PM
To: Rake Development and Discussion
Subject: Re: [Rake-devel] task - block vs. to_proc

 

Jay,

In Ruby there's an argument list, and a separate way to receive a block.
When you call a method with { ... }, it passes that as a block, separate
from the argument list. You can also pass a method/proc, etc in place of
a block, by putting it last on the argument list and prefixing with an
ampersand. 

Try this instead:

task task_name, &method(:setup_task).to_proc 

Assaf



On 6/11/07, Turpin, Jay <jay.turpin at intel.com> wrote:

I am probably missing something obvious, but I don 't understand why
this won' t work. I have created a Base Task class that I use to
simplify the creation of cus tom 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 as sign it
to the task:

class BaseTask < Rake::TaskLib

  attr_accessor :task_name

                

  def initialize(params=:my_task_name)

    @task_name = case params

      w hen 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_tas k() 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

  e nd

  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 


_______________________________________________
Rake-devel mailing list
Rake-devel at rubyforge.org
http://rubyforge.org/mailman/listinfo/rake-devel

 

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


More information about the Rake-devel mailing list