2012-05-02 28 views
8

my_gemハロー名1名2 NAME3くれThorで複数の引数やパラメータを指定する方法は?

を与えるmy_gemハローは、少なくとも1つの引数が必要です:こんにちはmy_gem名

を私はちょうどそれらを解析し、区切り文字で引数を区切るべきか?

例えば

my_gemハローNAME1、NAME2、NAME3、nameN

ファイルには、それは

class MyCLI < Thor 
    desc "hello NAMES", "say hello to names" 

    def hello(names) 
    say "hello #{names.split(',')}" 
    end 
end 

ようになりますか、これを行うためにとにかくがありますか?

答えて

12

はい、これを行う別の方法があります。

require 'thor' 
class TestApp < Thor 
    desc "hello NAMES", "long desc" 
    def hello(*names) 
     say "hello #{names.join('; ')}" 
    end 
end 

そして、それは次のように呼び出すことができます。また、スプラット演算子として知られています

$ thor test_app:hello first second third 
hello first; second; third 
+0

:http://stackoverflow.com/questions/4170037/what-does-the-star-mean -in-rubyとhttp://www.skorks.com/2009/08/method-arguments-in-ruby/ –

関連する問題