Skip to content

Commit 4a2e17f

Browse files
committed
100% documentation coverage.
1 parent f3758fd commit 4a2e17f

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

lib/memory/graph.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ module Memory
1111
module Graph
1212
IGNORE = Usage::IGNORE
1313

14-
# Represents a node in the object graph with usage information.
15-
class Node
16-
def initialize(object, usage = Usage.new, parent = nil, reference: nil)
17-
@object = object
18-
@usage = usage
19-
@parent = parent
14+
# Represents a node in the object graph with usage information.
15+
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.
21+
def initialize(object, usage = Usage.new, parent = nil, reference: nil)
22+
@object = object
23+
@usage = usage
24+
@parent = parent
2025
@children = nil
2126

2227
@reference = reference || parent&.find_reference(object)

lib/memory/usage.rb

Lines changed: 30 additions & 17 deletions
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
@@ -100,22 +106,29 @@ def self.of(root, seen: Set.new.compare_by_identity, ignore: IGNORE)
100106
end
101107
end
102108

103-
return new(size, count)
104-
end
105-
106-
def as_json(...)
107-
{
108-
size: @size,
109-
count: @count
110-
}
111-
end
112-
113-
def to_json(...)
114-
as_json.to_json(...)
115-
end
116-
117-
def to_s
118-
"(#{Memory.formatted_bytes(@size)} in #{@count} allocations)"
119-
end
109+
return new(size, count)
110+
end
111+
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.
115+
def as_json(...)
116+
{
117+
size: @size,
118+
count: @count
119+
}
120+
end
121+
122+
# Convert this usage to a JSON string.
123+
# @returns [String] JSON representation of this usage.
124+
def to_json(...)
125+
as_json.to_json(...)
126+
end
127+
128+
# Generate a human-readable string representation.
129+
# @returns [String] Formatted string showing size and allocation count.
130+
def to_s
131+
"(#{Memory.formatted_bytes(@size)} in #{@count} allocations)"
132+
end
120133
end
121134
end

0 commit comments

Comments
 (0)