Sha256: 80d195cc1437fe5f1463032d65420c3f56b3b66ed97a8736b118107344381b6b

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module Tcl
  module Ruby
    class ListArray
      Array.public_instance_methods(false).each do |name|
        next if name == '<<'
        define_method(name) do |*args, &block|
          @ary.send(name, *args, &block)
        end
      end

      def initialize
        @ary = []
      end

      def <<(buffer)
        @ary << buffer.dup unless buffer.empty?
        buffer.clear
        self
      end

      def to_string
        @ary.map! { |e| _to_string(e) }
        self
      end

      def to_list
        @ary.map { |e| _to_list(e) }.join(' ')
      end

      def map!(&block)
        @ary.map!(&block)
        self
      end

      def map(&block)
        r = dup
        r.map!(&block)
        r
      end

      def [](arg)
        if arg.is_a?(Range)
          r = dup
          r.ary = r.ary[arg]
          r
        else
          @ary.[](arg)
        end
      end

      protected

      attr_accessor :ary

      private

      def _to_string(str)
        if str[0] == '{' && str[-1] == '}'
          str = str[1..-2]
        elsif str[0] == '"' && str[-1] == '"'
          str = str[1..-2]
        end
        str
      end

      def _to_list(str)
        if str == '' || str.match(/\s/)
          "{#{str}}"
        else
          str
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tcl-ruby-0.1.0 lib/tcl/ruby/list_array.rb