Sha256: cd1bdddcbbc5650e6b325fb98786cbc46006275243d549a9fb350aaf0356e131

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'forwardable'

module Tcl
  module Ruby
    class ListArray
      extend Forwardable

      def_delegators :@ary, :find, :any?

      Array.public_instance_methods(false).each do |name|
        define_method(name) do |*args, &block|
          @ary.send(name, *args, &block)
        end
      end

      def uniq!(&block)
        @ary = @ary.reverse.uniq(&block).reverse
        self
      end

      def uniq(&block)
        dup.uniq!(&block)
      end

      def find_index_all
        raise ArgumentError unless block_given?
        r = []
        @ary.each_with_index do |e, idx|
          r << idx.to_s if yield(e)
        end
        r
      end

      def initialize(ary = [])
        @ary = Array(ary).map(&:to_s)
        @brackets = []
      end

      def clear
        @ary = []
        @brackets = []
      end

      def bracket_add(val)
        @brackets[@ary.size] ||= []
        @brackets[@ary.size] << val
      end

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

      def to_string
        @ary.map!(&:to_tcl_string)
        self
      end

      def to_list
        @ary.map(&:to_tcl_list).join(' ')
      end

      def replace
        @ary.size.times do |n|
          @ary[n] = yield(@ary[n]) unless @ary[n].brace?
        end
        # @ary.map! { |m| m.brace? ? m : yield(m) }
        self
      end

      def map!(&block)
        @ary.each(&:init)
        @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

      def to_h
        raise(TclArgumentError, 'list must have an even number of elements') if
          @ary.size.odd?
        Hash[@ary.each_slice(2).to_a]
      end

      protected

      attr_accessor :ary
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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