Skip to content

Commit da45de5

Browse files
committed
Add compatibility for to_h with block in Ruby 2.5
1 parent ed5ff5b commit da45de5

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lib/ostruct.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,21 @@ def initialize_copy(orig) # :nodoc:
122122
# data.to_h {|name, value| [name.to_s, value.upcase] }
123123
# # => {"country" => "AUSTRALIA", "capital" => "CANBERRA" }
124124
#
125-
def to_h(&block)
126-
if block_given?
127-
@table.to_h(&block)
128-
else
129-
@table.dup
125+
if {test: :to_h}.to_h{ [:works, true] }[:works] # RUBY_VERSION < 2.6 compatibility
126+
def to_h(&block)
127+
if block_given?
128+
@table.to_h(&block)
129+
else
130+
@table.dup
131+
end
132+
end
133+
else
134+
def to_h(&block)
135+
if block_given?
136+
@table.map(&block).to_h
137+
else
138+
@table.dup
139+
end
130140
end
131141
end
132142

0 commit comments

Comments
 (0)