Sha256: 649cfa9ef8b959127033a93029815bb62b155bacfe0b3ff470b9c19ef664a974

Contents?: true

Size: 1.28 KB

Versions: 10

Compression:

Stored size: 1.28 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 # Pair
end # SXP

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
sxp-0.1.3 lib/sxp/pair.rb
sxp-0.1.2 lib/sxp/pair.rb
sxp-0.1.0 lib/sxp/pair.rb
sxp-0.0.14 lib/sxp/pair.rb
sxp-0.0.13 lib/sxp/pair.rb
sxp-0.0.12 lib/sxp/pair.rb
sxp-0.0.11 lib/sxp/pair.rb
sxp-0.0.10 lib/sxp/pair.rb
sxp-0.0.9 lib/sxp/pair.rb
sxp-0.0.8 lib/sxp/pair.rb