Sha256: 8e34c7900c320682c3d29893cd08e977af417620867b40ac873e8a97e8d45f0e

Contents?: true

Size: 1.57 KB

Versions: 40

Compression:

Stored size: 1.57 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
      # Check the equality of a Lox String with another Lox object.
      # @param other [Datatype::LxString, String, Object]
      # @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
          False.instance
        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

40 entries across 40 versions & 1 rubygems

Version Path
loxxy-0.3.02 lib/loxxy/datatype/lx_string.rb
loxxy-0.3.01 lib/loxxy/datatype/lx_string.rb
loxxy-0.3.00 lib/loxxy/datatype/lx_string.rb
loxxy-0.2.06 lib/loxxy/datatype/lx_string.rb
loxxy-0.2.05 lib/loxxy/datatype/lx_string.rb
loxxy-0.2.04 lib/loxxy/datatype/lx_string.rb
loxxy-0.2.03 lib/loxxy/datatype/lx_string.rb
loxxy-0.2.02 lib/loxxy/datatype/lx_string.rb
loxxy-0.2.01 lib/loxxy/datatype/lx_string.rb
loxxy-0.2.00 lib/loxxy/datatype/lx_string.rb
loxxy-0.1.17 lib/loxxy/datatype/lx_string.rb
loxxy-0.1.16 lib/loxxy/datatype/lx_string.rb
loxxy-0.1.15 lib/loxxy/datatype/lx_string.rb
loxxy-0.1.14 lib/loxxy/datatype/lx_string.rb
loxxy-0.1.13 lib/loxxy/datatype/lx_string.rb
loxxy-0.1.12 lib/loxxy/datatype/lx_string.rb
loxxy-0.1.11 lib/loxxy/datatype/lx_string.rb
loxxy-0.1.10 lib/loxxy/datatype/lx_string.rb
loxxy-0.1.09 lib/loxxy/datatype/lx_string.rb
loxxy-0.1.08 lib/loxxy/datatype/lx_string.rb