Skip to content

Commit 474a953

Browse files
committed
100% documentation coverage.
1 parent f3758fd commit 474a953

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lib/memory/graph.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ module Graph
1313

1414
# Represents a node in the object graph with usage information.
1515
class Node
16+
# Initialize a new node in the object graph.
17+
# @parameter object [Object] The object this node represents.
18+
# @parameter usage [Usage] The memory usage of this object.
19+
# @parameter parent [Node | Nil] The parent node in the traversal tree.
20+
# @parameter reference [Symbol | Nil] The reference type from parent to this object.
1621
def initialize(object, usage = Usage.new, parent = nil, reference: nil)
1722
@object = object
1823
@usage = usage
@@ -153,11 +158,11 @@ def path
153158
def as_json(*)
154159
json = {
155160
path: path,
156-
object: {
157-
class: @object.class.name,
158-
object_id: @object.object_id
159-
},
160-
usage: @usage.as_json,
161+
object: {
162+
class: @object.class.name,
163+
object_id: @object.object_id
164+
},
165+
usage: @usage.as_json,
161166
}
162167

163168
if @children&.any?

lib/memory/usage.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
require "objspace"
1010

1111
module Memory
12+
# Tracks memory usage statistics including size and object count.
13+
#
14+
# Can be used to measure allocations or compute usage of object graphs.
1215
class Usage
16+
# Initialize a new usage tracker.
17+
# @parameter size [Integer] The total size in bytes.
18+
# @parameter count [Integer] The total count of objects.
1319
def initialize(size = 0, count = 0)
1420
@size = size
1521
@count = count
@@ -103,17 +109,24 @@ def self.of(root, seen: Set.new.compare_by_identity, ignore: IGNORE)
103109
return new(size, count)
104110
end
105111

112+
# Convert this usage to a JSON-compatible hash.
113+
# @parameter options [Hash | Nil] Optional JSON serialization options.
114+
# @returns [Hash] Hash with `:size` and `:count` keys.
106115
def as_json(...)
107116
{
108117
size: @size,
109-
count: @count
118+
count: @count
110119
}
111120
end
112121

122+
# Convert this usage to a JSON string.
123+
# @returns [String] JSON representation of this usage.
113124
def to_json(...)
114125
as_json.to_json(...)
115126
end
116127

128+
# Generate a human-readable string representation.
129+
# @returns [String] Formatted string showing size and allocation count.
117130
def to_s
118131
"(#{Memory.formatted_bytes(@size)} in #{@count} allocations)"
119132
end

0 commit comments

Comments
 (0)