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

ruby-changes:66010

From: Marc-Andre <ko1@a...>
Date: Wed, 28 Apr 2021 17:59:42 +0900 (JST)
Subject: [ruby-changes:66010] 7f1323c3bd (master): [ruby/ostruct] Add compatibility for to_h with block in Ruby 2.5

https://git.ruby-lang.org/ruby.git/commit/?id=7f1323c3bd

From 7f1323c3bd7d957740bbc5d2c7c0e1c9f167e501 Mon Sep 17 00:00:00 2001
From: Marc-Andre Lafortune <github@m...>
Date: Wed, 2 Sep 2020 16:05:59 -0400
Subject: [ruby/ostruct] Add compatibility for to_h with block in Ruby 2.5

https://github.com/ruby/ostruct/commit/da45de5068
---
 lib/ostruct.rb | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index 581dd73..1462901 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -166,11 +166,21 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L166
   #   data.to_h {|name, value| [name.to_s, value.upcase] }
   #               # => {"country" => "AUSTRALIA", "capital" => "CANBERRA" }
   #
-  def to_h(&block)
-    if block
-      @table.to_h(&block)
-    else
-      @table.dup
+  if {test: :to_h}.to_h{ [:works, true] }[:works] # RUBY_VERSION < 2.6 compatibility
+    def to_h(&block)
+      if block
+        @table.to_h(&block)
+      else
+        @table.dup
+      end
+    end
+  else
+    def to_h(&block)
+      if block
+        @table.map(&block).to_h
+      else
+        @table.dup
+      end
     end
   end
 
-- 
cgit v1.1


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

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