# Rake : conditional logic based on namespaces?

**URL:** <https://www.ruby-forum.com/t/rake-conditional-logic-based-on-namespaces/134738>\
**Category:** Ruby\
**Created:** [April 14, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/rake-conditional-logic-based-on-namespaces/134738 "2008-04-14T17:03:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dishkuvek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dishkuvek](https://www.ruby-forum.com/u/dishkuvek)\
**Post date:** [April 14, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/rake-conditional-logic-based-on-namespaces/134738/1 "2008-04-14T17:03:34Z")

</div>

Hi all

I’ve got a Rakefile with 2 groups of commands (split into namespaces).

One namespace (‘rake show:foo’) prints a string, the other ‘backticks’  
it (“rake run:foo”’).

The same functionality is duplicated in each namespace, so much that  
the code smells  
to high heaven - I’m in danger of ‘show’ and ‘do’ getting out of step.

An example might help:

* * *

require ‘rake’

ls = ‘ls /tmp’  
date = ‘date’

# show commands

namespace :show do  
desc ‘show command to list /tmp’  
task :ls do  
puts ls  
end

```
   desc 'show command to print date'
   task :date do
       puts date
   end

```

end

desc ‘show commands’  
task :show =\> [‘show:ls’, ‘show:date’]

# run command

namespace :run do  
desc ‘list /tmp’  
task :ls do  
puts `#{ls}`  
end

```
   desc 'print date'
   task :date do
       puts `#{date}`
   end

```

end

desc ‘run commands’  
task :run =\> [‘run:ls’, ‘run:date’]

* * *

There must be someway to prevent duplicating the actual commands to run  
(which are a lot more convoluted in reality) - any ideas how?

Thanks a lot.
