我是Ruby新手。我的一個練習是去除箭頭編程並引發異常。我不斷收到undefined method for validate_region_and_shape
。Ruby:獲取未定義的方法錯誤NoMethodError
def self.classify(region, shape)
# Alternative for raising exception within classify method
#raise Error_Message unless CLASSIFICATIONS.include? (region)
#raise Error_Message unless CLASSIFICATIONS[region].include? (shape)
if validate_region_and_shape(region, shape)
places = CLASSIFICATIONS[region][shape]
"You have a(n) '#{places}'"
end
end
def validate_region_and_shape(region, shape)
raise Error_Message if valid_region?
raise Error_Message if valid_shape?
end
def valid_region?
CLASSIFICATIONS.include?(region)
end
def valid_shape?
CLASSIFICATIONS[region].include?(shape)
end
end
任何幫助將不勝感激。
你的代碼不能編譯,你有一個太多的'end'。縮進使得它看起來像'def'在def self.classify'裏面。你也大幅改變了代碼,新代碼沒有你原來的問題。目前還不清楚你在問什麼。 – Schwern
另外,應該'valid_region?'方法將'region'作爲一個arg,同樣'valid_shape'要''形狀'? – Rashmirathi