2012-08-17 59 views
0

混淆的描述,我知道请看例子:雷神宝石 - 发送参数任务之前

Foo < Thor 

    desc "bar","bar method" 
    def bar 
    puts "Hello from bar #{options[:id]}" 
    end 

    desc "nar","nar method" 
    def nar 
    puts "Hello from nar" 
    end 
end 

这是非常简单的。所以,如果我打电话(thor已经被设置为使用类名作为第一标识符)。现在没有身份证了,所以没有打印任何东西。

foo bar 
> Hello from bar 
foo nar 
> Hello from nar 

最后问题,我怎样才能使用Thor能够发送参数给这种形式的方法?

foo 12 bar 
> Hello from bar 12 
foo nar 
> Hello from nar 

我想要做的是在任务名称之前传递参数栏,Thor有可能吗?

对不起,令人困惑的问题,但目前简化我的复杂的代码的最佳途径。

+0

据我所知,有没有办法任务名称不是Thor中的第一个参数。这是你要解决的要求,还是我误解了你的问题? – workergnome 2012-08-20 19:46:48

回答

0

你有红宝石的功能,让您可以:

class Foo < Thor 
    desc "bar","bar method" 
    def bar(id) 
    puts "Hello from bar #{id}" 
    end 

    def method_missing(m, *args, &block) 
    if m.is_a? Integer ## Might not work. May need to_i and an exception check. 
     bar(m) 
    end 
    end 
end 

这是从我的头顶,所以你的里程可能会有所不同...