Skip to content

Commit 6b94aaf

Browse files
Adds Tms#to_h (#4)
1 parent 81ac521 commit 6b94aaf

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/benchmark.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,20 @@ def to_a
527527
[@label, @utime, @stime, @cutime, @cstime, @real]
528528
end
529529

530+
#
531+
# Returns a hash containing the same data as `to_a`.
532+
#
533+
def to_h
534+
{
535+
label: @label,
536+
utime: @utime,
537+
stime: @stime,
538+
cutime: @cutime,
539+
cstime: @cstime,
540+
real: @real
541+
}
542+
end
543+
530544
protected
531545

532546
#

test/benchmark/test_benchmark.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,13 @@ def test_realtime_output
155155
realtime = Benchmark.realtime { sleep sleeptime }
156156
assert_operator sleeptime, :<, realtime
157157
end
158+
159+
# Test that `to_h` returns a hash with the expected data.
160+
def test_tms_to_h
161+
tms = Benchmark::Tms.new(1.1, 2.2, 3.3, 4.4, 5.5, 'my label')
162+
expected_hash = {
163+
utime: 1.1, stime: 2.2, cutime: 3.3, cstime: 4.4, real: 5.5, label: 'my label'
164+
}
165+
assert_equal(expected_hash, tms.to_h)
166+
end
158167
end

0 commit comments

Comments
 (0)