summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-12-22 17:05:03 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-12-22 17:05:03 +0000
commit1e30df6f77c79b07d1c65e450b0167861c180473 (patch)
tree68ad1f210e24af5733e27ed086bf9796fb6777a3 /lib
parent8ef2aae2d0350506fb1b12eb0afd56b5b1f51b89 (diff)
ostruct.rb: Accept block for to_h [#15451].
Patch by Shuji Kobayashi. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66496 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/ostruct.rb17
1 files changed, 15 insertions, 2 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index 67ba043535..a758a65979 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -105,15 +105,28 @@ class OpenStruct
end
#
+ # call-seq:
+ # ostruct.to_h -> hash
+ # ostruct.to_h {|name, value| block } -> hash
+ #
# Converts the OpenStruct to a hash with keys representing
# each attribute (as symbols) and their corresponding values.
#
+ # If a block is given, the results of the block on each pair of
+ # the receiver will be used as pairs.
+ #
# require "ostruct"
# data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
# data.to_h # => {:country => "Australia", :capital => "Canberra" }
+ # data.to_h {|name, value| [name.to_s, value.upcase] }
+ # # => {"country" => "AUSTRALIA", "capital" => "CANBERRA" }
#
- def to_h
- @table.dup
+ def to_h(&block)
+ if block_given?
+ @table.to_h(&block)
+ else
+ @table.dup
+ end
end
#