Skipoura Skipoura - 2 months ago 6
Ruby Question

Ruby: Getting undefined method errors NoMethodError

I'm new to Ruby. One of my exercise to remove Arrowhead programming and raise an exception. I keep getting

undefined method for validate_region_and_shape
.

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


Any help would be highly appreciated.

Answer

Do you just have a typo when defining validate_region_and_shape?

Edit:

If you need to get rid of exceptions, then you could try something like

def validate_region_and_shape(region, shape)
  CLASSIFICATIONS.include?(region) && CLASSIFICATIONS[region].include?(shape)
end

def 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)
    arrowhead = CLASSIFICATIONS[region][shape]
    "You have a(n) '#{arrowhead}' arrowhead. Probably priceless."
  else
   raise Error_Message
  end
end