Sha256: 25fe3b9bfba36b1918e7b94e592dfefc7d05c63932e5b737f3b46e8cb53dcc40

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

module Basic101

  class BasicString < BasicObject

    include Comparable
    include BasicComparisons

    attr_reader :value

    def initialize(value)
      @value = value.to_s
    end

    def <=>(other)
      return nil unless other.is_a?(self.class)
      value <=> other.value
    end

    def print_string(output)
      output.print @value
    end

    def print_new_line(output)
      output.print "\n"
    end

    def to_string
      self
    end

    def left(count)
      count = count.to_i
      if count < 0
        raise InvalidArgumentError
      end
      BasicString.new(@value[0...count])
    end

    def right(count)
      count = count.to_i
      if count < 0
        raise InvalidArgumentError
      end
      substring = @value[/.{0,#{count}}$/]
      BasicString.new(substring)
    end

    def length
      BasicInteger.new(@value.size)
    end

    def mid(start, count = nil)
      start = start.to_i
      count = count && count.to_i
      raise InvalidArgumentError if start < 1
      raise InvalidArgumentError if count && count < 1
      start -= 1
      substring = if count
                    @value[start, count]
                  else
                    @value[start..-1]
                  end
      self.class.new(substring)
    end

    def +(other)
      BasicString.new(value + other.to_string.value)
    end

    def asc
      raise InvalidArgumentError if @value.empty?
      BasicInteger.new(@value.chars.first.ord)
    end

    def val
      BasicFloat.new(@value.to_f).simplest
    end

    def str
      self
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
basic101-0.4.0 lib/basic101/basic_string.rb
basic101-0.2.0 lib/basic101/basic_string.rb
basic101-0.1.0 lib/basic101/basic_string.rb