Question

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?

Was it helpful?

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

OTHER TIPS

Following the answers provided, maybe it would make sense to keep the keywords in a constant if you don't need to mutate them.

class Blah
  BLACKLISTED_KEYWORDS = %w(acquista acquistiamo acquisto acquistano compro compriamo comprano)

  private

  def item_valid?
    keywords = BLACKLISTED_KEYWORDS
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top