faker-ruby/faker · error · ArgumentError

Filters do not match any sciences

Error message

Filters do not match any sciences

What it means

Raised by Faker::Science.science when the combination of branch filters produces an empty set of sciences. BRANCHES maps each filter (:empirical, :formal, :natural, :social, :basic, :applied) to a list of science categories, and the filters are intersected; contradictory pairs like :empirical + :formal (empirical covers empirical_* categories, formal covers formal_*) share no categories, so selected becomes empty and ArgumentError is raised. Unknown branch symbols are silently ignored (BRANCHES.key? guard), so they never trigger this error.

Source

Thrown at lib/faker/default/science.rb:37

      # @see https://en.wikipedia.org/wiki/Science#Branches_of_science
      # @see Faker::Educator.subject
      #
      # @param branches [Array<Symbol>]
      # @return [String]
      #
      # @example
      #   Faker::Science.science #=> "Space science"
      #   Faker::Science.science(:natural, :applied) #=> "Engineering"
      #   Faker::Science.science(:formal, :applied) #=> "Computer Science"
      #
      # @faker.version next
      def science(*branches)
        selected = BRANCHES.values.flatten.uniq
        branches.each do |branch|
          selected &= BRANCHES[branch] if BRANCHES.key? branch
        end

        raise ArgumentError, 'Filters do not match any sciences' if selected.empty?

        sciences = []
        selected.each do |branch|
          sciences += translate("faker.science.branch.#{branch}")
        end

        sample(sciences)
      end

      ##
      # Produces the name of a element.
      #
      # @return [String]
      #
      # @example
      #   Faker::Science.element #=> "Carbon"
      #
      # @faker.version 1.8.5

View on GitHub (pinned to cca4184947)

Solutions

  1. Use compatible filter combinations: at most one branch-type (:empirical or :formal) combined with at most one modifier (:natural/:social and/or :basic/:applied), e.g. science(:natural, :applied).
  2. Call science with no arguments when you just want any science.
  3. Guard compound selections before calling by intersecting Faker::Science::BRANCHES values yourself and skipping empty results.

Example fix

# before
Faker::Science.science(:empirical, :formal) # ArgumentError: contradictory filters

# after
Faker::Science.science(:formal, :applied) # 'Computer Science'
Defensive patterns

Strategy: validation

Validate before calling

BRANCHES = Faker::Science::BRANCHES
selected = BRANCHES.values.flatten.uniq
branches.each { |b| selected &= BRANCHES[b] if BRANCHES.key?(b) }
Faker::Science.science(*branches) unless selected.empty?

Type guard

def compatible_science_filters?(branches)
  b = Faker::Science::BRANCHES
  selected = b.values.flatten.uniq
  branches.each { |branch| selected &= b[branch] if b.key?(branch) }
  !selected.empty?
end

Try / catch

begin
  Faker::Science.science(*branches)
rescue ArgumentError
  Faker::Science.science # unfiltered
end

Prevention

When it happens

Trigger: Faker::Science.science(:empirical, :formal) — the two filters intersect to an empty list and raise. Also :formal + :natural (natural only contains empirical_natural_* categories) raises. A single filter or none never raises.

Common situations: Building filter combinations programmatically from UI checkboxes and hitting mutually exclusive pairs; assuming any combination of valid filters is valid; not realizing misspelled filters (e.g. :nature) are ignored rather than validated, so the error only signals contradictory valid filters.

Related errors


AI-assisted analysis of faker-ruby/faker@cca4184947 (2026-08-21). Data as JSON: /api/errors/c469629eab599465. Report an issue: GitHub.