Skip to content

Commit 097b77e

Browse files
committed
Fix usage of numeric.
1 parent 0aeff8a commit 097b77e

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

lib/memory/usage.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ def self.of(root)
6161
size += ObjectSpace.memsize_of(object)
6262

6363
# Add the object's reachable objects to the queue:
64-
queue.concat(ObjectSpace.reachable_objects_from(object))
64+
if reachable_objects = ObjectSpace.reachable_objects_from(object)
65+
queue.concat(reachable_objects)
66+
end
6567
end
6668

6769
return new(size, count)

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Handle `Memory::Usage.of(number)` without error.
6+
37
## v0.8.2
48

59
- Fix several formatting issues.

test/memory/usage.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Copyright, 2022-2025, by Samuel Williams.
55

66
require "memory/usage"
7+
require "json"
78

89
describe Memory::Usage do
910
let(:usage) {subject.new}
@@ -139,6 +140,22 @@
139140
size: be > 0
140141
)
141142
end
143+
144+
it "can compute usage of nil" do
145+
usage = subject.of(nil)
146+
expect(usage).to have_attributes(
147+
count: be == 0,
148+
size: be == 0
149+
)
150+
end
151+
152+
it "can compute usage of numbers" do
153+
usage = subject.of(42)
154+
expect(usage).to have_attributes(
155+
count: be == 1,
156+
size: be == 0
157+
)
158+
end
142159
end
143160

144161
with "#to_s" do

0 commit comments

Comments
 (0)