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:
parent
37b445eaeb
commit
7f1323c3bd
1 changed files with 15 additions and 5 deletions
|
@ -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:
|
||||||
|
|
Loading…
Reference in a new issue