我是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