2012-08-17 22 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 

這是從我的頭頂,所以你的里程可能會有所不同...