Skip to content

Class#name override causes ArgumentError when iterating classes #3148

@amkisko

Description

@amkisko

Description

When calling klass.name on Faker::Travel::Airport (and potentially other Faker classes), an ArgumentError is raised because the method requires keyword arguments (:size and :region) that are not provided.

This breaks the standard Ruby behavior where Class#name should return the class name as a string.

Steps to Reproduce

require 'faker'

# This should return "Faker::Travel::Airport" but raises ArgumentError
Faker::Travel::Airport.name
# => ArgumentError: missing keywords: :size, :region

Expected Behavior

Faker::Travel::Airport.name
# => "Faker::Travel::Airport"

Actual Behavior

Faker::Travel::Airport.name
# => ArgumentError: missing keywords: :size, :region

Impact

This affects any code that:

  • Iterates over classes using ObjectSpace.each_object(Class)
  • Calls klass.name expecting standard Ruby behavior
  • Uses metaprogramming that relies on Class#name

For example:

ObjectSpace.each_object(Class) do |klass|
  name = klass.name  # Raises ArgumentError when klass is Faker::Travel::Airport
  # ...
end

Environment

  • Ruby version: 3.4.7
  • Faker version: 3.5.2
  • OS: macOS

Suggested Fix

The Faker gem should ensure that:

  1. The class method name returns the class name string (standard Ruby behavior)
  2. Instance methods that generate fake data use different method names (e.g., airport_name instead of name)
  3. If the class method must be overridden, it should properly delegate to Module#name

Workaround

Calling code can wrap klass.name in error handling, but this is a workaround for a bug in Faker:

begin
  name = klass.name
rescue ArgumentError, NoMethodError, NameError
  next
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions