Sha256: d9dcbeed86677fc22d1766a3b52c51f473ac6737d7a3df01fb059fd20c3cf6a1

Contents?: true

Size: 1.3 KB

Versions: 7

Compression:

Stored size: 1.3 KB

Contents

# -*- encoding: utf-8 -*-
module SXP
  ##
  class Pair
    # @return [Object]
    attr_accessor :head

    # @return [Object]
    attr_accessor :tail

    ##
    # @param  [Object] head
    # @param  [Object] tail
    def initialize(head = nil, tail = nil)
      @head, @tail = head, tail
    end

    ##
    # Returns `true` if the head and tail of this pair are both `nil`.
    #
    # @return [Boolean]
    def empty?
      head.nil? && tail.nil?
    end

    ##
    # Returns `true` if the tail of this pair is not `nil` or another pair.
    #
    # @return [Boolean]
    # @see    http://srfi.schemers.org/srfi-1/srfi-1.html#ImproperLists
    def dotted?
      !proper?
    end

    ##
    # Returns `true` if the tail of this pair is `nil` or another pair.
    #
    # @return [Boolean]
    # @see    http://srfi.schemers.org/srfi-1/srfi-1.html#ImproperLists
    def proper?
      tail.nil? || tail.is_a?(Pair)
    end

    ##
    # Returns an array representation of this pair.
    #
    # @return [Array]
    def to_a
      [head, tail]
    end

    ##
    # Returns a developer-friendly representation of this pair.
    #
    # @return [String]
    def inspect
      case
        when tail.nil?
          "(#{head.inspect})"
        else
          "(#{head.inspect} . #{tail.inspect})"
      end
    end
  end # Pair
end # SXP

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sxp-1.1.0 lib/sxp/pair.rb
sxp-1.0.2 lib/sxp/pair.rb
sxp-1.0.1 lib/sxp/pair.rb
sxp-1.0.0 lib/sxp/pair.rb
sxp-1.0.0.beta1 lib/sxp/pair.rb
sxp-0.1.5 lib/sxp/pair.rb
sxp-0.1.4 lib/sxp/pair.rb