2017-02-28 9 views
-2

このコードを実行していますが、エラーが発生しています。Rubyエラー:引数の数が正しくありません(0の場合は1)(ArgumentError)

ここでコード: -

class Text 
    def post(success, error) 
     if authenticate?(@user, @password) 
      success.call 
     else 
      erro.call 
     end 
    end 
end 
text = Text.new('Ruby Bits!') 
success = ->{ puts "Sent!"} 
error = ->{ raise 'Auth error'} 
text.post(success,error) 

を教えてください。この問題を解決するには?

+0

'以来POST'メソッドの定義がある二つのパラメータ、あなた」 dは 'post'に2つの引数を渡す必要があります:' test.post(success、error) ' – Surya

+0

私は試しています。このエラーは削除されません。同じエラーを表示 – test

+0

"Ruby Bits!"という引数を渡しています。 'Text'の初期化子に渡しますが、引数はありません。 – jordanm

答えて

0

ArgumentErrorがどの行番号から発生しているかを詳しく調べます(25行目)。

1つの引数をText#initializeに渡していますが、1つの引数を取るinitializeのバージョンを定義していません。

代わりにこれを試してみてください(デフォルトを呼び出し、ゼロ引数のテキストのコンストラクタ):

class Text 
    def post(success, error) 
     if authenticate?(@user, @password) 
      success.call 
     else 
      error.call 
     end 
    end 
end 
text = Text.new 
success = ->{ puts "Sent!"} 
error = ->{ raise 'Auth error'} 
text.post(success, error) 

または1つの引数で初期化定義:

class Text 
    def initialize(your_meaningful_argument) 
     # do stuff 
    end 

    def post(success, error) 
     if authenticate?(@user, @password) 
      success.call 
     else 
      error.call 
     end 
    end 
end 
+0

エラーを表示します: - 'post ':未定義のメソッド' authenticate?' for#(NoMethodError) \tレベル-1.rbから:012:

test

+0

はい、インスタンスメソッドを認証しようとしていますか?あなたのTextクラスのインスタンスでは、そのメソッドを定義していません。 –

+0

定義する必要があります: classテキスト def authenticate? #do stuff end end '' ' –

関連する問題