Skip to content

Commit

Permalink
Add compatibility for to_h with block in Ruby 2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Sep 2, 2020
1 parent ed5ff5b commit da45de5
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/ostruct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,21 @@ def initialize_copy(orig) # :nodoc:
# data.to_h {|name, value| [name.to_s, value.upcase] }
# # => {"country" => "AUSTRALIA", "capital" => "CANBERRA" }
#
def to_h(&block)
if block_given?
@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_given?
@table.to_h(&block)
else
@table.dup
end
end
else
def to_h(&block)
if block_given?
@table.map(&block).to_h
else
@table.dup
end
end
end

Expand Down

0 comments on commit da45de5

Please sign in to comment.