1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/ostruct] Add compatibility for to_h with block in Ruby 2.5

https://github.com/ruby/ostruct/commit/da45de5068
This commit is contained in:
Marc-Andre Lafortune 2020-09-02 16:05:59 -04:00 committed by Hiroshi SHIBATA
parent 37b445eaeb
commit 7f1323c3bd
No known key found for this signature in database
GPG key ID: F9CF13417264FAC2

View file

@ -166,11 +166,21 @@ class OpenStruct
# data.to_h {|name, value| [name.to_s, value.upcase] } # data.to_h {|name, value| [name.to_s, value.upcase] }
# # => {"country" => "AUSTRALIA", "capital" => "CANBERRA" } # # => {"country" => "AUSTRALIA", "capital" => "CANBERRA" }
# #
def to_h(&block) if {test: :to_h}.to_h{ [:works, true] }[:works] # RUBY_VERSION < 2.6 compatibility
if block def to_h(&block)
@table.to_h(&block) if block
else @table.to_h(&block)
@table.dup else
@table.dup
end
end
else
def to_h(&block)
if block
@table.map(&block).to_h
else
@table.dup
end
end end
end end