ruby/ruby · error · ArgumentError

dependency name must be a String, was #{name.inspect}

Error message

dependency name must be a String, was #{name.inspect}

What it means

Gem::Dependency.new validates its first argument: a String is required (a Regexp is still accepted with a deprecation warning). Anything else — Symbol, Gem::Version, Integer, nil — raises ArgumentError with the received value in the message.

Source

Thrown at lib/rubygems/dependency.rb:44

  ##
  # Allows you to force this dependency to be a prerelease.

  attr_writer :prerelease

  ##
  # Constructs a dependency with +name+ and +requirements+. The last
  # argument can optionally be the dependency type, which defaults to
  # <tt>:runtime</tt>.

  def initialize(name, *requirements)
    case name
    when String then # ok
    when Regexp then
      msg = ["NOTE: Dependency.new w/ a regexp is deprecated.",
             "Dependency.new called from #{Gem.location_of_caller.join(":")}"]
      warn msg.join("\n") unless Gem::Deprecate.skip
    else
      raise ArgumentError,
            "dependency name must be a String, was #{name.inspect}"
    end

    type         = Symbol === requirements.last ? requirements.pop : :runtime
    requirements = requirements.first if requirements.length == 1 # unpack

    unless TYPES.include? type
      raise ArgumentError, "Valid types are #{TYPES.inspect}, " \
                           "not #{type.inspect}"
    end

    @name        = name
    @requirement = Gem::Requirement.create requirements
    @type        = type
    @prerelease  = false

    # This is for Marshal backwards compatibility. See the comments in
    # +requirement+ for the dirty details.

View on GitHub (pinned to 0e5b888e1c)

Solutions

  1. Convert before constructing: `Gem::Dependency.new(name.to_s, *reqs)`.
  2. Check the `inspect` output in the message to see the actual object that arrived, then fix the call site (wrong variable or wrong order).
  3. Coerce external input early: `name = String(name)` after validating it is String/Symbol.

Example fix

# before
dep = Gem::Dependency.new(:rake, ">= 13.0")
# after
dep = Gem::Dependency.new("rake", ">= 13.0")
Defensive patterns

Strategy: type-guard

Validate before calling

raise ArgumentError, "dependency name must be a String" unless name.is_a?(String)
dep = Gem::Dependency.new(name, *reqs)

Type guard

def valid_dependency_name?(name)
  name.is_a?(String)
end

Prevention

When it happens

Trigger: `Gem::Dependency.new(:rake, ">= 13")`; spec.add_dependency called with a symbol variable; swapped argument order so a Gem::Version or nil lands in the name slot.

Common situations: Metaprogramming where names became symbols; helper APIs accepting strings-or-symbols and passing through; refactors from Bundler dependency objects; typos in name/version order.

Related errors


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