class Gem::Requirement - Documentation for Ruby 4.0
Description: class Gem::Requirement: A Requirement is a set of one or more version restrictions. tt>) different restriction operators.
Keywords: ruby,class,Gem::Requirement

🔍

No recent searches

# class Gem::Requirement

A `Requirement` is a set of one or more version restrictions. It supports a few (`=, !=, >, <, >=, <=, ~>`) different restriction operators.

See `Gem::Version` for a description on how versions and requirements work together in RubyGems.

### Constants

DefaultPrereleaseRequirement

  The default requirement matches any version

DefaultRequirement

  The default requirement matches any non-prerelease version

PATTERN

  A regular expression that matches a requirement

### Public Class Methods

create (*inputs)
Source

    # File lib/rubygems/requirement.rb, line 55
    def self.create(*inputs)
      return new inputs if inputs.length > 1

      input = inputs.shift

      case input
      when Gem::Requirement then
        input
      when Gem::Version, Array then
        new input
      when "!" then
        source_set
      else
        if input.respond_to? :to_str
          new [input.to_str]
        else
          default
        end
      end
    end

Factory method to create a `Gem::Requirement` object. Input may be a Version, a `String`, or nil. Intended to simplify client code.

If the input is “weird”, the default version requirement is returned.

default ()
Source

    # File lib/rubygems/requirement.rb, line 76
    def self.default
      new ">= 0"
    end

default_prerelease ()
Source

    # File lib/rubygems/requirement.rb, line 80
    def self.default_prerelease
      new ">= 0.a"
    end

new (*requirements)
Source

    # File lib/rubygems/requirement.rb, line 133
    def initialize(*requirements)
      requirements = requirements.flatten
      requirements.compact!
      requirements.uniq!

      if requirements.empty?
        @requirements = [DefaultRequirement]
      else
        @requirements = requirements.map! {|r| self.class.parse r }
      end
    end

Constructs a requirement from `requirements`. Requirements can be Strings, Gem::Versions, or Arrays of those. `nil` and duplicate requirements are ignored. An empty set of `requirements` is the same as `">= 0"`.

parse (obj)
Source

    # File lib/rubygems/requirement.rb, line 103
    def self.parse(obj)
      return ["=", obj] if Gem::Version === obj

      unless PATTERN =~ obj.to_s
        raise BadRequirementError, "Illformed requirement [#{obj.inspect}]"
      end
      op = -($1 || "=")
      version = -$2

      if op == ">=" && version == "0"
        DefaultRequirement
      elsif op == ">=" && version == "0.a"
        DefaultPrereleaseRequirement
      else
        [op, Gem::Version.new(version)]
      end
    end

Parse `obj`, returning an `[op, version]` pair. `obj` can be a `String` or a `Gem::Version`.

If `obj` is a `String`, it can be either a full requirement specification, like `">= 1.2"`, or a simple version number, like `"1.2"`.

    parse("> 1.0")                 # => [">", Gem::Version.new("1.0")]
    parse("1.0")                   # => ["=", Gem::Version.new("1.0")]
    parse(Gem::Version.new("1.0")) # => ["=,  Gem::Version.new("1.0")]

### Public Instance Methods

=== (version)
Alias for: satisfied_by?
=~ (version)
Alias for: satisfied_by?
concat (new)
Source

    # File lib/rubygems/requirement.rb, line 148
    def concat(new)
      new = new.flatten
      new.compact!
      new.uniq!
      new = new.map {|r| self.class.parse r }

      @requirements.concat new
    end

Concatenates the `new` requirements onto this requirement.

exact? ()
Source

    # File lib/rubygems/requirement.rb, line 186
    def exact?
      return false unless @requirements.size == 1
      @requirements[0][0] == "="
    end

true if the requirement is for only an exact version

none? ()
Source

    # File lib/rubygems/requirement.rb, line 175
    def none?
      if @requirements.size == 1
        @requirements[0] == DefaultRequirement
      else
        false
      end
    end

true if this gem has no requirements.

prerelease? ()
Source

    # File lib/rubygems/requirement.rb, line 228
    def prerelease?
      requirements.any? {|r| r.last.prerelease? }
    end

A requirement is a prerelease if any of the versions inside of it are prereleases

satisfied_by? (version)
Source

    # File lib/rubygems/requirement.rb, line 241
    def satisfied_by?(version)
      raise ArgumentError, "Need a Gem::Version: #{version.inspect}" unless
        Gem::Version === version
      requirements.all? {|op, rv| OPS.fetch(op).call version, rv }
    end

True if `version` satisfies this `Requirement`.

Also aliased as: ===, =~
specific? ()
Source

    # File lib/rubygems/requirement.rb, line 253
    def specific?
      return true if @requirements.length > 1 # GIGO, > 1, > 2 is silly

      !%w[> >=].include? @requirements.first.first # grab the operator
    end

True if the requirement will not always match the latest version.

### Protected Instance Methods

_sorted_requirements ()
Source

    # File lib/rubygems/requirement.rb, line 279
    def _sorted_requirements
      @_sorted_requirements ||= requirements.sort_by(&:to_s)
    end

_tilde_requirements ()
Source

    # File lib/rubygems/requirement.rb, line 283
    def _tilde_requirements
      @_tilde_requirements ||= _sorted_requirements.select {|r| r.first == "~>" }
    end