2017-02-28 1 views
-2

이 코드를 실행 중이지만 오류가 발생합니다. 여기루비 오류 : 잘못된 인수 수 (1은 0) (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'에 두 개의 인수를 전달해야합니다 :'test.post (success, error)' – Surya

+0

나는 시험해 보았다. 이 오류는 제거되지 않습니다. 동일한 오류 표시 – test

+0

arg "Ruby Bits!"를 전달 중입니다. 'Text'의 이니셜 라이저로 보내지 만 인자를 취하지 않습니다. – jordanm

답변

0

ArgumentError가 발생한 행 번호 (사용자의 경우 25 행)를 자세히 살펴보십시오.

하나의 인수를 Text # initialize에 전달했지만 하나의 인수를 사용하는 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) 

또는 하나 개의 인수로 초기화 정의 :

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 level-1.rb : 30 : '

' – test

+0

네, 이제 인스턴스 메소드를 호출하려고합니까? Text 클래스의 인스턴스에서 해당 메서드를 정의하지 않았습니다. –

+0

정의해야합니다 : 클래스 텍스트 def authenticate? # do stuff end end ''' –

관련 문제