From 1e30df6f77c79b07d1c65e450b0167861c180473 Mon Sep 17 00:00:00 2001 From: marcandre Date: Sat, 22 Dec 2018 17:05:03 +0000 Subject: 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 --- NEWS | 7 +++++++ lib/ostruct.rb | 17 +++++++++++++++-- spec/ruby/library/openstruct/to_h_spec.rb | 7 +++++++ test/ostruct/test_ostruct.rb | 7 +++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 17f03af6b0..02b12f3d82 100644 --- a/NEWS +++ b/NEWS @@ -239,6 +239,13 @@ sufficient information, see the ChangeLog file or Redmine * Numeric#step now returns an instance of Enumerator::ArithmeticSequence class rather than one of Enumerator class. +[OpenStruct] + + [Modified methods] + + * OpenStruct#to_h now maps keys and values to new keys and values + by the block if given. [Feature #15143] + [Proc] [New methods] diff --git a/lib/ostruct.rb b/lib/ostruct.rb index 67ba043535..a758a65979 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -104,16 +104,29 @@ class OpenStruct @table = @table.dup 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 # diff --git a/spec/ruby/library/openstruct/to_h_spec.rb b/spec/ruby/library/openstruct/to_h_spec.rb index f3e157816b..1a8a87a94d 100644 --- a/spec/ruby/library/openstruct/to_h_spec.rb +++ b/spec/ruby/library/openstruct/to_h_spec.rb @@ -26,4 +26,11 @@ describe "OpenStruct#to_h" do @to_h[:age] = 71 @os.age.should == 70 end + + ruby_version_is "2.6" do + it "converts [key, value] pairs returned by the block to a hash" do + h = @os.to_h {|key, value| [key.to_s, value * 2]} + h.should == {"name" => "John SmithJohn Smith", "age" => 140, "pension" => 600} + end + end end diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb index d89bc94cc3..61a4822810 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -141,6 +141,13 @@ class TC_OpenStruct < Test::Unit::TestCase assert_equal(h, OpenStruct.new("name" => "John Smith", "age" => 70, pension: 300).to_h) end + def test_to_h_with_block + os = OpenStruct.new("country" => "Australia", :capital => "Canberra") + assert_equal({"country" => "AUSTRALIA", "capital" => "CANBERRA" }, + os.to_h {|name, value| [name.to_s, value.upcase]}) + assert_equal("Australia", os.country) + end + def test_each_pair h = {name: "John Smith", age: 70, pension: 300} os = OpenStruct.new(h) -- cgit v1.2.3