Class: Sheap::HeapObjectCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Collection
Defined in:
lib/sheap.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Collection

#arrays, #bignums, #callcaches, #callinfos, #class_named, #classes, #complexes, #constcaches, #crefs, #datas, #files, #floats, #hashes, #icasses, #imemos, #instances_of, #iseqs, #matches, #ments, #modules, #of_imemo_type, #of_type, #plain_objects, #rationals, #regexps, #strings, #structs, #symbols

Constructor Details

#initialize(objects, heap = nil) ⇒ HeapObjectCollection

Returns a new instance of HeapObjectCollection.



139
140
141
142
143
# File 'lib/sheap.rb', line 139

def initialize(objects, heap = nil)
  objects = objects.to_a unless objects.instance_of?(Array)
  @objects = objects
  @heap = heap || objects.first&.heap
end

Instance Attribute Details

#heapObject (readonly)

Returns the value of attribute heap.



137
138
139
# File 'lib/sheap.rb', line 137

def heap
  @heap
end

#objectsObject (readonly)

Returns the value of attribute objects.



137
138
139
# File 'lib/sheap.rb', line 137

def objects
  @objects
end

Instance Method Details

#[](*args) ⇒ Object



158
159
160
# File 'lib/sheap.rb', line 158

def [](*args)
  @objects[*args]
end

#count(&block) ⇒ Object



178
179
180
# File 'lib/sheap.rb', line 178

def count(&block)
  @objects.count(&block)
end

#each(&block) ⇒ Object



170
171
172
# File 'lib/sheap.rb', line 170

def each(&block)
  @objects.each(&block)
end

#filter(&block) ⇒ Object Also known as: select



145
146
147
# File 'lib/sheap.rb', line 145

def filter(&block)
  HeapObjectCollection.new(@objects.select(&block), @heap)
end

#inspectObject



199
200
201
# File 'lib/sheap.rb', line 199

def inspect
  "#<#{self.class} (#{size} objects)>"
end

#last(n = nil) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/sheap.rb', line 162

def last(n = nil)
  if n
    HeapObjectCollection.new(@objects.last(n))
  else
    objects.last
  end
end

#lengthObject Also known as: size



174
175
176
# File 'lib/sheap.rb', line 174

def length
  @objects.length
end

#pretty_print(q) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/sheap.rb', line 182

def pretty_print(q)
  q.group(1, '[', ']') {
    if size <= 20
      q.seplist(self) {|v|
        q.pp v
      }
    else
      preview = 4
      q.seplist(first(preview)) {|v|
        q.pp v
      }
      q.comma_breakable
      q.text "... (#{size - preview} more)"
    end
  }
end

#sample(n = nil) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/sheap.rb', line 150

def sample(n = nil)
  if n
    HeapObjectCollection.new(@objects.sample(n))
  else
    @objects.sample
  end
end

#to_aObject Also known as: to_ary



203
204
205
# File 'lib/sheap.rb', line 203

def to_a
  @objects
end