diff options
-rw-r--r-- | lib/benchmark.rb | 14 | ||||
-rw-r--r-- | test/benchmark/test_benchmark.rb | 9 |
2 files changed, 23 insertions, 0 deletions
diff --git a/lib/benchmark.rb b/lib/benchmark.rb index 5ce9710586..4cfc0d8a5b 100644 --- a/lib/benchmark.rb +++ b/lib/benchmark.rb @@ -527,6 +527,20 @@ module Benchmark [@label, @utime, @stime, @cutime, @cstime, @real] end + # + # Returns a hash containing the same data as `to_a`. + # + def to_h + { + label: @label, + utime: @utime, + stime: @stime, + cutime: @cutime, + cstime: @cstime, + real: @real + } + end + protected # diff --git a/test/benchmark/test_benchmark.rb b/test/benchmark/test_benchmark.rb index 0eb331a1cd..3030bc5dec 100644 --- a/test/benchmark/test_benchmark.rb +++ b/test/benchmark/test_benchmark.rb @@ -155,4 +155,13 @@ BENCH realtime = Benchmark.realtime { sleep sleeptime } assert_operator sleeptime, :<, realtime end + + # Test that `to_h` returns a hash with the expected data. + def test_tms_to_h + tms = Benchmark::Tms.new(1.1, 2.2, 3.3, 4.4, 5.5, 'my label') + expected_hash = { + utime: 1.1, stime: 2.2, cutime: 3.3, cstime: 4.4, real: 5.5, label: 'my label' + } + assert_equal(expected_hash, tms.to_h) + end end |