[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rake task help with DRYness

Alex Wayne

4/3/2008 8:58:00 PM

I'm somewhat new to writing my own rake tasks. I have a pair of tasks
that convert some stuff from one format to another.

rake my_ns:to_a
rake my_ns:to_b

And everything works just fine. The problem is that the implementation
the code is basically duplicated. The process is the same, the only
real difference is that old_format and new_format local variables are
swapped in the second conversion task. I want to make this much DRYer
by refactoring the common code out.

Now, if this was a ruby class, I would simply create a private method
called convert_to(format), or something similar. Then the format
conversion happens in this method, based on the argument.

How does one accomplish this with rake tasks? Basically I want to pass
arguments to a rake task, or method of some sort, and use the result. A
simple task in general programming, but I am not sure how to make this
work in the context of rake.

Thanks for the help.
--
Posted via http://www.ruby-....

2 Answers

Joel VanderWerf

4/3/2008 9:27:00 PM

0

Alex Wayne wrote:
> I'm somewhat new to writing my own rake tasks. I have a pair of tasks
> that convert some stuff from one format to another.
>
> rake my_ns:to_a
> rake my_ns:to_b
>
> And everything works just fine. The problem is that the implementation
> the code is basically duplicated. The process is the same, the only
> real difference is that old_format and new_format local variables are
> swapped in the second conversion task. I want to make this much DRYer
> by refactoring the common code out.
>
> Now, if this was a ruby class, I would simply create a private method
> called convert_to(format), or something similar. Then the format
> conversion happens in this method, based on the argument.
>
> How does one accomplish this with rake tasks? Basically I want to pass
> arguments to a rake task, or method of some sort, and use the result. A
> simple task in general programming, but I am not sure how to make this
> work in the context of rake.
>
> Thanks for the help.

Not sure I understand, but why not just do what you said: define a
method that handles the common work?

task :to_a do |name|
convert(name)
end

task :to_b do |name|
convert(name)
end

def convert name
puts "converting #{name}"
end

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Alex Wayne

4/3/2008 9:34:00 PM

0

Joel VanderWerf wrote:
> Alex Wayne wrote:
>> by refactoring the common code out.
>> Thanks for the help.
> Not sure I understand, but why not just do what you said: define a
> method that handles the common work?
>
> task :to_a do |name|
> convert(name)
> end
>
> task :to_b do |name|
> convert(name)
> end
>
> def convert name
> puts "converting #{name}"
> end

*Slaps forehead*. For some reason, I assumed that is not how a rake
task works. I guess I need to remember its just plain old ruby with
some syntax sugar, right?
--
Posted via http://www.ruby-....