Module: AnnotatedArray

Defined in:
lib/rbbt/annotations/annotated_array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#list_idObject

Returns the value of attribute list_id.



2
3
4
# File 'lib/rbbt/annotations/annotated_array.rb', line 2

def list_id
  @list_id
end

Instance Method Details

#[](pos, clean = false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rbbt/annotations/annotated_array.rb', line 20

def [](pos, clean = false)

  value = super(pos)
  return value if value.nil? or clean

  value = value.dup if value.frozen?

  value = annotate(value)

  value.extend AnnotatedArray if Array === value and Annotated === value

  if value.respond_to? :container
    value.container       = self
    value.container_index = pos
  end

  value
end

#collect(&block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rbbt/annotations/annotated_array.rb', line 76

def collect(&block)

  if block_given?

    res = []
    each do |value|
      res << yield(value)
    end

    res
  else

    res = []
    each do |value|
      res << value
    end

    res
  end
end

#double_arrayObject



8
9
10
# File 'lib/rbbt/annotations/annotated_array.rb', line 8

def double_array
  AnnotatedArray === self.send(:[], 0, true)
end

#each(&block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rbbt/annotations/annotated_array.rb', line 39

def each(&block)

  pos = 0
  super do |value|

    case value
    when Array
      value = value.dup if value.frozen?

      value = annotate(value)

      value.extend AnnotatedArray if Array === value and Annotated === value

      value.container       = self
      value.container_index = pos

      pos += 1

      block.call value
    when String

      value = value.dup if value.frozen?

      value = annotate(value)

      value.container       = self
      value.container_index = pos

      pos += 1

      block.call value
    else
      block.call value
    end
  end
end

#firstObject



12
13
14
# File 'lib/rbbt/annotations/annotated_array.rb', line 12

def first
  self[0]
end

#lastObject



16
17
18
# File 'lib/rbbt/annotations/annotated_array.rb', line 16

def last
  self[-1]
end

#rejectObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rbbt/annotations/annotated_array.rb', line 98

def reject
  res = []

  each do |value|
    res << value unless yield(value)
  end

  annotate(res)
  res.extend AnnotatedArray

  res
end

#remove(list) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/rbbt/annotations/annotated_array.rb', line 134

def remove(list)

  res = (self - list)

  annotate(res)
  res.extend AnnotatedArray

  res
end

#selectObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rbbt/annotations/annotated_array.rb', line 111

def select
  res = []

  each do |value|
    res << value if yield(value)
  end

  annotate(res)
  res.extend AnnotatedArray

  res
end

#select_by(method, *args, &block) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rbbt/annotations/annotated_array.rb', line 153

def select_by(method, *args, &block)
  return [] if self.empty? and not self.respond_to? :annotate

  values = self.send(method, *args)
  values = values.clean_annotations if values.respond_to? :clean_annotations

  new = []
  if block_given?
    self.clean_annotations.each_with_index do |e,i|
      new << e if yield(values[i])
    end
  else
    self.clean_annotations.each_with_index do |e,i|
      new << e if values[i]
    end
  end
  self.annotate(new)
  new.extend AnnotatedArray

  new
end

#sort(&block) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/rbbt/annotations/annotated_array.rb', line 144

def sort(&block)
  res = self.collect.sort(&block).collect{|value| value.respond_to?(:clean_annotations) ? value.clean_annotations.dup : value.dup }

  annotate(res)
  res.extend AnnotatedArray

  res
end

#subset(list) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/rbbt/annotations/annotated_array.rb', line 124

def subset(list)

  res = (self & list)

  annotate(res)
  res.extend AnnotatedArray

  res
end

#to_aObject



4
5
6
# File 'lib/rbbt/annotations/annotated_array.rb', line 4

def to_a
  self.collect{|i| i }
end