Class: Tcl::Ruby::ListArray
- Inherits:
-
Object
- Object
- Tcl::Ruby::ListArray
- Extended by:
- Forwardable
- Defined in:
- lib/tcl/ruby/list_array.rb
Instance Method Summary collapse
- #<<(buffer) ⇒ Object
- #[](arg) ⇒ Object
- #bracket_add(val) ⇒ Object
- #clear ⇒ Object
- #find_index_all ⇒ Object
-
#initialize(ary = []) ⇒ ListArray
constructor
A new instance of ListArray.
- #map(&block) ⇒ Object
- #map!(&block) ⇒ Object
- #replace ⇒ Object
- #to_h ⇒ Object
- #to_list ⇒ Object
- #to_string ⇒ Object
- #uniq(&block) ⇒ Object
- #uniq!(&block) ⇒ Object
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 |
#clear ⇒ Object
39 40 41 42 |
# File 'lib/tcl/ruby/list_array.rb', line 39 def clear @ary = [] @brackets = [] end |
#find_index_all ⇒ Object
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 |
#replace ⇒ Object
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_h ⇒ Object
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_list ⇒ Object
61 62 63 |
# File 'lib/tcl/ruby/list_array.rb', line 61 def to_list @ary.map(&:to_tcl_list).join(' ') end |
#to_string ⇒ Object
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 |