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,6 +166,7 @@ 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" }
# #
if {test: :to_h}.to_h{ [:works, true] }[:works] # RUBY_VERSION < 2.6 compatibility
def to_h(&block) def to_h(&block)
if block if block
@table.to_h(&block) @table.to_h(&block)
@ -173,6 +174,15 @@ class OpenStruct
@table.dup @table.dup
end end
end end
else
def to_h(&block)
if block
@table.map(&block).to_h
else
@table.dup
end
end
end
# #
# :call-seq: # :call-seq: