# Rake namespace selection

**URL:** <https://www.ruby-forum.com/t/rake-namespace-selection/168326>\
**Category:** Ruby\
**Created:** [May 19, 2009, 5:17pm UTC](https://www.ruby-forum.com/t/rake-namespace-selection/168326 "2009-05-19T17:17:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [May 19, 2009, 5:17pm UTC](https://www.ruby-forum.com/t/rake-namespace-selection/168326/1 "2009-05-19T17:17:01Z")

</div>

is there a way to “explicitly” invoke a block inside the scope of a  
parent or explicit “root relative” rake namespace instead of always  
being a child of the current namespace ???

namespace :xxx do  
namespace :yyy do  
namespace …

```
      ... called_in_unknown_namespace(arg);

end

```

end  
end

def called\_in\_unknown\_namespace(arg)

# execute below block as if it were in “root” namespace

namespace ‘root namespace’ do

```
# declare additional item to do in root namespace task
task :task_in_root do
  puts('invoked by "rake task_in_root"');
end

```

end

# scope restored to wherever you were previously

end

Thanks!

---

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [May 19, 2009, 8:41pm UTC](https://www.ruby-forum.com/t/rake-namespace-selection/168326/2 "2009-05-19T20:41:18Z")

</div>

Peter Pk wrote:

> is there a way to “explicitly” invoke a block inside the scope of a  
> parent or explicit “root relative” rake namespace instead of always  
> being a child of the current namespace ???
> 
> namespace :xxx do  
> namespace :yyy do  
> namespace …
> 
> ```
> ... called_in_unknown_namespace(arg);
> 
> end
> 
> ```
> 
> end  
> end
> 
> def called\_in\_unknown\_namespace(arg)
> 
> # execute below block as if it were in “root” namespace
> 
> namespace ‘root namespace’ do
> 
> ```
> # declare additional item to do in root namespace task
> task :task_in_root do
> puts('invoked by "rake task_in_root"');
> end
> 
> ```
> 
> end
> 
> # scope restored to wherever you were previously
> 
> end
> 
> Thanks!

Ok - figured it out looking at the source, since all tasks simply have  
the “scope name” prepended to their name, and the “namespace” is just a  
list of names that defines the currnet scope scope in the task manager,  
then I was able to do the below to explicitly selct the “global scope”.  
building the list from a scope name ie: ‘:name1:name2:name3’, ‘^name’  
using the same format as a task name reference will make it general.  
BTW what would be appropriate to specify an “absolute path” in rake  
convention ??? since namespace :xxx specifies a relative _child_ - ie:  
desirably somethign like file paths ‘/’ and ‘…’ maybe ‘::’ ???

module Rake  
module TaskManager  
def in\_explicit\_namespace(name)  
oldscope = @scope;  
@scope = Array.new();  
# build scope name list from name here  
ns = NameSpace.new(self,@scope);  
yield(ns)  
ns  
ensure  
@scope = oldscope;  
end  
end  
end

# down below in a rakefile

namespace :num1 do  
namespace :num2 do  
Rake.application.in\_explicit\_namespace ‘:’ do  
puts(“in explicit block”);

```
  task :x do |t|
    puts(" doing explicit task x");
  end
end

```

end  
end

# from command line

$ rake x  
doing explicit task x  
$
