# Rake namespace selection

**URL:** <https://rubytalk.org/t/rake-namespace-selection/53473>\
**Category:** ruby-talk\
**Created:** [19 May 2009 15:17 UTC](https://rubytalk.org/t/rake-namespace-selection/53473 "2009-05-19T15:17:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Pk](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@Peter\_Pk](https://rubytalk.org/u/Peter_Pk)\
**Post date:** [19 May 2009 15:17 UTC](https://rubytalk.org/t/rake-namespace-selection/53473/1 "2009-05-19T15:17:03Z")

</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  
&nbsp;&nbsp;namespace :yyy do  
&nbsp;&nbsp;&nbsp;&nbsp;namespace ...

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... called\_in\_unknown\_namespace(arg);

&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

def called\_in\_unknown\_namespace(arg)

&nbsp;&nbsp;# execute below block as if it were in "root" namespace  
&nbsp;&nbsp;namespace 'root namespace' do

&nbsp;&nbsp;&nbsp;&nbsp;# declare additional item to do in root namespace task  
&nbsp;&nbsp;&nbsp;&nbsp;task :task\_in\_root do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts('invoked by "rake task\_in\_root"');  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;# scope restored to wherever you were previously

end

Thanks!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Peter\_Pk](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@Peter\_Pk](https://rubytalk.org/u/Peter_Pk)\
**Post date:** [19 May 2009 18:41 UTC](https://rubytalk.org/t/rake-namespace-selection/53473/2 "2009-05-19T18:41:26Z")

</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  
> &nbsp;&nbsp;namespace :yyy do  
> &nbsp;&nbsp;&nbsp;&nbsp;namespace ...
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... called\_in\_unknown\_namespace(arg);
> 
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> def called\_in\_unknown\_namespace(arg)
> 
> &nbsp;&nbsp;# execute below block as if it were in "root" namespace  
> &nbsp;&nbsp;namespace 'root namespace' do
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# declare additional item to do in root namespace task  
> &nbsp;&nbsp;&nbsp;&nbsp;task :task\_in\_root do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts('invoked by "rake task\_in\_root"');  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;# 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  
&nbsp;&nbsp;module TaskManager  
&nbsp;&nbsp;&nbsp;&nbsp;def in\_explicit\_namespace(name)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oldscope = @scope;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@scope = Array.new();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# build scope name list from name here  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ns = NameSpace.new(self,@scope);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(ns)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ns  
&nbsp;&nbsp;&nbsp;&nbsp;ensure  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@scope = oldscope;  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

# down below in a rakefile

namespace :num1 do  
&nbsp;&nbsp;namespace :num2 do  
&nbsp;&nbsp;&nbsp;&nbsp;Rake.application.in\_explicit\_namespace ':' do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts("in explicit block");

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

# from command line

$ rake x  
&nbsp;&nbsp;doing explicit task x  
$

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
