Sha256: 52ce343db81a147bc355f559d75d77052508294a5193044e142fa912b418a92b

Contents?: true

Size: 1.32 KB

Versions: 50

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require 'singleton' # Use the Singleton design pattern
require_relative 'builtin_datatype'

module Loxxy
  module Datatype
    # Class for representing a Lox nil "value".
    class Nil < BuiltinDatatype
      include Singleton # Make a singleton class

      # Build the sole instance
      def initialize
        super(nil)
      end

      # Check the equality with another object.
      # @param other [Datatype::BuiltinDatatype, NilClass, Object]
      # @return [Datatype::Boolean]
      def ==(other)
        is_nil = other.kind_of?(Nil) || other.kind_of?(NilClass)
        is_nil ? True.instance : False.instance
      end

      # Is the value considered falsey in Lox?
      # Rule: false and nil are falsey and everything else is truthy.
      # This test used in conditional statements (i.e. if, while)
      def falsey?
        true
      end

      # Is the value considered truthy in Lox?
      # Rule: false and nil are falsey and everything else is truthy.
      # This test used in conditional statements (i.e. if, while)
      def truthy?
        false
      end

      # Method called from Lox to obtain the text representation of nil.
      # @return [String]
      def to_str
        'nil'
      end
    end # class

    Nil.instance.freeze # Make the sole instance immutable
  end # module
end # module

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
loxxy-0.4.08 lib/loxxy/datatype/nil.rb
loxxy-0.4.07 lib/loxxy/datatype/nil.rb
loxxy-0.4.06 lib/loxxy/datatype/nil.rb
loxxy-0.4.05 lib/loxxy/datatype/nil.rb
loxxy-0.4.04 lib/loxxy/datatype/nil.rb
loxxy-0.4.03 lib/loxxy/datatype/nil.rb
loxxy-0.4.02 lib/loxxy/datatype/nil.rb
loxxy-0.4.01 lib/loxxy/datatype/nil.rb
loxxy-0.4.00 lib/loxxy/datatype/nil.rb
loxxy-0.3.03 lib/loxxy/datatype/nil.rb
loxxy-0.3.02 lib/loxxy/datatype/nil.rb
loxxy-0.3.01 lib/loxxy/datatype/nil.rb
loxxy-0.3.00 lib/loxxy/datatype/nil.rb
loxxy-0.2.06 lib/loxxy/datatype/nil.rb
loxxy-0.2.05 lib/loxxy/datatype/nil.rb
loxxy-0.2.04 lib/loxxy/datatype/nil.rb
loxxy-0.2.03 lib/loxxy/datatype/nil.rb
loxxy-0.2.02 lib/loxxy/datatype/nil.rb
loxxy-0.2.01 lib/loxxy/datatype/nil.rb
loxxy-0.2.00 lib/loxxy/datatype/nil.rb