[前][次][番号順一覧][スレッド一覧]

ruby-changes:65187

From: Keith <ko1@a...>
Date: Mon, 8 Feb 2021 13:06:40 +0900 (JST)
Subject: [ruby-changes:65187] 3a7ff66abc (master): [ruby/benchmark] Adds `Tms#to_h`

https://git.ruby-lang.org/ruby.git/commit/?id=3a7ff66abc

From 3a7ff66abc0c389851ab3c5cd0ac53ebe0647eec Mon Sep 17 00:00:00 2001
From: Keith Bennett <keithrbennett@g...>
Date: Sun, 7 Feb 2021 23:02:52 -0500
Subject: [ruby/benchmark] Adds `Tms#to_h`

[Feature #17601]
---
 lib/benchmark.rb                 | 14 ++++++++++++++
 test/benchmark/test_benchmark.rb |  9 +++++++++
 2 files changed, 23 insertions(+)

diff --git a/lib/benchmark.rb b/lib/benchmark.rb
index 5ce9710..4cfc0d8 100644
--- a/lib/benchmark.rb
+++ b/lib/benchmark.rb
@@ -527,6 +527,20 @@ module Benchmark https://github.com/ruby/ruby/blob/trunk/lib/benchmark.rb#L527
       [@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 0eb331a..3030bc5 100644
--- a/test/benchmark/test_benchmark.rb
+++ b/test/benchmark/test_benchmark.rb
@@ -155,4 +155,13 @@ BENCH https://github.com/ruby/ruby/blob/trunk/test/benchmark/test_benchmark.rb#L155
     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
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]