[Rake-devel] task - block vs. to_proc

Assaf Arkin assaf at labnotes.org
Tue Jun 12 01:05:58 EDT 2007


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 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*
>
>
> _______________________________________________
> 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/20070611/138b756a/attachment.html 


More information about the Rake-devel mailing list