Sha256: 270ddc63163134e4e5e00976ece5aad62e487c59fa0a3fb8315f4e77e227080a

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

class Code
  class Object
    class String < ::Code::Object
      attr_reader :raw

      def initialize(string)
        @raw = string
      end

      def call(**args)
        operator = args.fetch(:operator, nil)
        arguments = args.fetch(:arguments, [])

        if operator == "to_function"
          to_function(arguments)
        elsif operator == "+"
          plus(arguments)
        elsif operator == "*"
          multiplication(arguments)
        elsif operator == "reverse"
          reverse(arguments)
        else
          super
        end
      end

      def succ
        ::Code::Object::String.new(raw.succ)
      end

      def to_sym
        raw.to_sym
      end

      def to_s
        raw
      end

      def inspect
        raw.inspect
      end

      private

      def to_function(arguments)
        sig(arguments)
        Code.evaluate("(_) => { _.#{raw} }")
      end

      def plus(arguments)
        sig(arguments, ::Code::Object::String)
        other = arguments.first.value
        ::Code::Object::String.new(raw + other.raw)
      end

      def multiplication(arguments)
        sig(arguments, ::Code::Object::Number)
        other = arguments.first.value
        ::Code::Object::String.new(raw * other.raw)
      end

      def reverse(arguments)
        sig(arguments)
        ::Code::Object::String.new(raw.reverse)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
code-ruby-0.2.4 lib/code/object/string.rb
template-ruby-0.2.4 lib/code/object/string.rb