Sha256: d80d080743b908fff57c7f0122310ab3d5c943424bfc56d7ba5f78b45f9910c7

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

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 # class Pair
end # module SXP

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sxp-0.0.7 lib/sxp/pair.rb
sxp-0.0.6 lib/sxp/pair.rb
sxp-0.0.5 lib/sxp/pair.rb
sxp-0.0.4 lib/sxp/pair.rb