Search code examples
rubynomethoderror

Exception: undefined method `blacklisted_keywords'


I have the following block of Ruby code within a class:

def self.blacklisted_keywords
  %w(acquista acquistiamo acquisto acquistano compro compriamo comprano)
end

private

def item_valid?
  keywords = blacklisted_keywords
end

Why can't I call blacklisted_keywords without getting: "undefined method `blacklisted_keywords'"? What am I doing wrong?


Solution

  • Because blacklisted_keywords is not an instance method, rather a class method.keywords = blacklisted_keywords means ruby looking at it implicitly as keywords = self.blacklisted_keywords. That causes an error, as it is not an instance method. Replace keywords = blacklisted_keywords as keywords = self.class.blacklisted_keywords