aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ostruct.rb
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2020-09-02 16:05:59 -0400
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2021-04-28 17:48:05 +0900
commit7f1323c3bd7d957740bbc5d2c7c0e1c9f167e501 (patch)
tree1eccfb67e499776b0061da5c121cc4e0e865b88f /lib/ostruct.rb
parent37b445eaeb9fdb5c717a32253d0d00c50a650b93 (diff)
downloadruby-7f1323c3bd7d957740bbc5d2c7c0e1c9f167e501.tar.gz
[ruby/ostruct] Add compatibility for to_h with block in Ruby 2.5
https://github.com/ruby/ostruct/commit/da45de5068
Diffstat (limited to 'lib/ostruct.rb')
-rw-r--r--lib/ostruct.rb20
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index 581dd73ea7..1462901d06 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -166,11 +166,21 @@ class OpenStruct
# 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