[][src]Constant spinoso_env::RUBY_API_POLYFILLS

pub const RUBY_API_POLYFILLS: &str = "# frozen_string_literal: true\n\nENV = Object.new\n\nclass << ENV\n  def [](name)\n    @backend ||= ::Artichoke::Environ.new\n    @backend[name]\n  end\n\n  def []=(name, value)\n    @backend ||= ::Artichoke::Environ.new\n    @backend[name] = value\n  end\n\n  def assoc(name)\n    name = name.to_str unless name.is_a?(String)\n    value = self[name]\n    return nil if value.nil?\n\n    [name, value]\n  end\n\n  def clear\n    to_h.each do |var_name, var_value|\n      self[var_name] = nil unless var_value.nil?\n    end\n\n    to_h\n  end\n\n  def delete(name)\n    value = self[name]\n    if value.nil?\n      yield name if block_given?\n      return nil\n    end\n\n    self[name] = nil\n    yield name if block_given?\n\n    value\n  end\n\n  def delete_if\n    return to_enum(:delete_if) unless block_given?\n\n    to_h.each do |key, value|\n      delete(key) if yield key, value\n    end\n    to_h\n  end\n\n  def each(&blk)\n    return to_enum(:each) unless block_given?\n\n    to_h.each(&blk)\n  end\n\n  def each_key(&blk)\n    return to_enum(:each_key) unless block_given?\n\n    to_h.each_key(&blk)\n  end\n\n  def each_pair(&blk)\n    return to_enum(:each_pair) unless block_given?\n\n    to_h.each_pair(&blk)\n  end\n\n  def each_value(&blk)\n    return to_enum(:each_value) unless block_given?\n\n    to_h.each_value(&blk)\n  end\n\n  def empty?\n    to_h.empty?\n  end\n\n  def fetch(name, default = (not_set = true))\n    warn \'warning: block supersedes default value argument\' if !not_set && block_given?\n\n    name =\n      if name.is_a?(String)\n        name\n      elsif name.respond_to?(:to_str)\n        converted = name.to_str\n        unless converted.is_a?(String)\n          raise TypeError, \"can\'t convert #{name.class} to String (#{name.class}#to_str gives #{converted.class})\"\n        end\n\n        converted\n      else\n        cls = name.class\n        cls = \'nil\' if name.nil?\n\n        raise TypeError, \"no implicit conversion of #{cls} into String\"\n      end\n\n    value = self[name]\n    return value unless value.nil?\n    return yield name if block_given?\n    return default unless not_set\n\n    raise KeyError.new(\"key not found: #{name.inspect}\", receiver: self, key: name)\n  end\n\n  def filter(&blk)\n    return to_enum(:filter) unless block_given?\n\n    to_h.select(&blk)\n  end\n\n  def filter!(&blk)\n    return to_enum(:filter!) unless block_given?\n\n    select!(&blk)\n  end\n\n  def has_key?(name) # rubocop:disable Naming/PredicateName\n    to_h.key?(name)\n  end\n\n  def has_value?(value) # rubocop:disable Naming/PredicateName\n    to_h.value?(value)\n  end\n\n  def include?(name)\n    to_h.key?(name)\n  end\n\n  def index(value)\n    to_h.key(value)\n  end\n\n  def inspect\n    to_h.to_s\n  end\n\n  def invert\n    to_h.invert\n  end\n\n  def keep_if\n    return to_enum(:keep_if) unless block_given?\n\n    to_h.each do |key, value|\n      delete(key) unless yield key, value\n    end\n    to_h\n  end\n\n  def key(value)\n    to_h.key(value)\n  end\n\n  def key?(name)\n    !self[name].nil?\n  end\n\n  def keys\n    to_h.keys\n  end\n\n  def length\n    to_h.length\n  end\n\n  def member?(name)\n    !self[name].nil?\n  end\n\n  def rassoc(value)\n    value = value.to_str unless value.is_a?(String)\n    to_h.each do |k, v|\n      return [k, v] if v == value\n    end\n    nil\n  end\n\n  def rehash\n    nil\n  end\n\n  def reject(&blk)\n    return to_enum(:reject) unless block_given?\n\n    to_h.delete_if(&blk)\n  end\n\n  def reject!\n    return to_enum(:reject!) unless block_given?\n\n    modified = false\n    to_h.each do |key, value|\n      if yield key, value\n        delete(key)\n        modified = true\n      end\n    end\n\n    return self if modified\n\n    nil\n  end\n\n  def replace(hash)\n    hash.each do |k, v|\n      self[k] = v\n    end\n    select! { |k, _| hash.key?(k) }\n  end\n\n  def select(&blk)\n    return to_enum(:select) unless block_given?\n\n    to_h.select(&blk)\n  end\n\n  def select!(&blk)\n    return to_enum(:select!) unless block_given?\n\n    env = to_h\n    # collect all the keys where the block evaluates to false\n    to_remove = env.reject(&blk)\n\n    # returns nil if no changes were made\n    return nil if to_remove.empty?\n\n    to_remove.each do |key, _|\n      delete(key)\n    end\n    self\n  end\n\n  def shift\n    envs = to_h\n    return nil if envs.nil? || envs.empty?\n\n    name, value = envs.shift\n    self[name] = nil\n    [name, value]\n  end\n\n  def size\n    to_h.size\n  end\n\n  def slice(*keys)\n    to_h.slice(*keys)\n  end\n\n  def store(name, value)\n    self[name] = value\n  end\n\n  def to_a\n    to_h.to_a\n  end\n\n  def to_h\n    @backend ||= ::Artichoke::Environ.new\n    h = @backend.to_h\n    return h unless block_given?\n\n    pairs = h.each_pair.map do |name, value|\n      tx = yield(name, value)\n      if tx.is_a?(Array)\n        raise ArgumentError, \"element has wrong array length (expected 2, was #{tx.length})\" if tx.length != 2\n\n        tx\n      elsif tx.respond_to?(:to_ary)\n        pair = tx.to_ary\n        unless pair.is_a?(Array)\n          raise TypeError, \"can\'t convert #{tx.class} to Array (#{tx.class}#to_ary gives #{pair.class})\"\n        end\n\n        pair\n      else\n        raise TypeError, \"wrong element type #{tx.class} (expected array)\"\n      end\n    end\n    pairs.to_h\n  end\n\n  def to_hash\n    to_h\n  end\n\n  def to_s\n    \'ENV\'\n  end\n\n  def update(hash)\n    hash.each do |key, value|\n      value = yield(key, self[key], value) if block_given? && key?(key)\n      self[key] = value\n    end\n\n    to_h\n  end\n\n  def value?(name)\n    to_h.value?(name)\n  end\n\n  def values\n    to_h.values\n  end\n\n  def values_at(*names)\n    to_h.values_at(*names)\n  end\nend\n";

Ruby implementations of the full ENV API.

This crate only implements enough of the ENV API in native code to glue the system APIs to the interpreter. ENV offers a rich, Hash-like API on top of the raw names and values.

This constant contains Ruby source code that implements the remainder of these APIs on top of the core accessors and mutators in this crate.