Sha256: 5a060db76323e6e99659700a6e4a480d24f055b6aa862a35a6243f1046d07763

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require_relative 'false'
require_relative 'true'

module Loxxy
  module Datatype
    # Class for representing a Lox string of characters value.
    class LXString < BuiltinDatatype
      # Compare a Lox String with another Lox (or genuine Ruby) String
      # @param other [Datatype::LxString, String]
      # @return [Datatype::Boolean]
      def ==(other)
        case other
        when LXString
          (value == other.value) ? True.instance : False.instance
        when String
          (value == other) ? True.instance : False.instance
        else
          err_msg = "Cannot compare a #{self.class} with #{other.class}"
          raise StandardError, err_msg
        end
      end

      # Perform the concatenation of two Lox stings or
      # one Lox string and a Ruby String
      # @param other [Loxxy::Datatype::LXString, String]
      # @return [Loxxy::Datatype::LXString]
      def +(other)
        case other
        when LXString
          self.class.new(value + other.value)
        when Numeric
          self.class.new(value + other)
        else
          err_msg = "`+': #{other.class} can't be coerced into #{self.class} (TypeError)"
          raise TypeError, err_msg
        end
      end

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

      protected

      def validated_value(aValue)
        unless aValue.is_a?(String)
          raise StandardError, "Invalid number value #{aValue}"
        end

        # Remove double quotes delimiter
        aValue.gsub(/(^")|("$)/, '')
      end
    end # class
  end # module
end # module

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
loxxy-0.0.15 lib/loxxy/datatype/lx_string.rb
loxxy-0.0.14 lib/loxxy/datatype/lx_string.rb