Rake task help with DRYness

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.

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

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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs