Class: Tcl::Ruby::ListArray

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tcl/ruby/list_array.rb

Instance Method Summary collapse

Constructor Details

#initialize(ary = []) ⇒ ListArray

Returns a new instance of ListArray.



34
35
36
37
# File 'lib/tcl/ruby/list_array.rb', line 34

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

Instance Method Details

#<<(buffer) ⇒ Object



49
50
51
52
53
54
# File 'lib/tcl/ruby/list_array.rb', line 49

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

#[](arg) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/tcl/ruby/list_array.rb', line 85

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

#bracket_add(val) ⇒ Object



44
45
46
47
# File 'lib/tcl/ruby/list_array.rb', line 44

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

#clearObject



39
40
41
42
# File 'lib/tcl/ruby/list_array.rb', line 39

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

#find_index_allObject

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
# File 'lib/tcl/ruby/list_array.rb', line 25

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

#map(&block) ⇒ Object



79
80
81
82
83
# File 'lib/tcl/ruby/list_array.rb', line 79

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

#map!(&block) ⇒ Object



73
74
75
76
77
# File 'lib/tcl/ruby/list_array.rb', line 73

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

#replaceObject



65
66
67
68
69
70
71
# File 'lib/tcl/ruby/list_array.rb', line 65

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

#to_hObject

Raises:



95
96
97
98
99
# File 'lib/tcl/ruby/list_array.rb', line 95

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

#to_listObject



61
62
63
# File 'lib/tcl/ruby/list_array.rb', line 61

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

#to_stringObject



56
57
58
59
# File 'lib/tcl/ruby/list_array.rb', line 56

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

#uniq(&block) ⇒ Object



21
22
23
# File 'lib/tcl/ruby/list_array.rb', line 21

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

#uniq!(&block) ⇒ Object



16
17
18
19
# File 'lib/tcl/ruby/list_array.rb', line 16

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