# Rake task help with DRYness

**URL:** <https://www.ruby-forum.com/t/rake-task-help-with-dryness/133817>\
**Category:** Ruby\
**Created:** [April 3, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/rake-task-help-with-dryness/133817 "2008-04-03T22:58:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [April 3, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/rake-task-help-with-dryness/133817/1 "2008-04-03T22:58:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [April 3, 2008, 11:28pm UTC](https://www.ruby-forum.com/t/rake-task-help-with-dryness/133817/2 "2008-04-03T23:28:55Z")

</div>

Alex W. 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

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [April 3, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/rake-task-help-with-dryness/133817/3 "2008-04-03T23:33:39Z")

</div>

Joel VanderWerf wrote:

> Alex W. 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?
