ruby-changes:54287
From: marcandre <ko1@a...>
Date: Sun, 23 Dec 2018 02:05:19 +0900 (JST)
Subject: [ruby-changes:54287] marcandRe: r66496 (trunk): ostruct.rb: Accept block for to_h [#15451].
marcandre 2018-12-23 02:05:03 +0900 (Sun, 23 Dec 2018) New Revision: 66496 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66496 Log: ostruct.rb: Accept block for to_h [#15451]. Patch by Shuji Kobayashi. Modified files: trunk/NEWS trunk/lib/ostruct.rb trunk/spec/ruby/library/openstruct/to_h_spec.rb trunk/test/ostruct/test_ostruct.rb Index: lib/ostruct.rb =================================================================== --- lib/ostruct.rb (revision 66495) +++ lib/ostruct.rb (revision 66496) @@ -105,15 +105,28 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L105 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 # Index: test/ostruct/test_ostruct.rb =================================================================== --- test/ostruct/test_ostruct.rb (revision 66495) +++ test/ostruct/test_ostruct.rb (revision 66496) @@ -141,6 +141,13 @@ class TC_OpenStruct < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ostruct/test_ostruct.rb#L141 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) Index: spec/ruby/library/openstruct/to_h_spec.rb =================================================================== --- spec/ruby/library/openstruct/to_h_spec.rb (revision 66495) +++ spec/ruby/library/openstruct/to_h_spec.rb (revision 66496) @@ -26,4 +26,11 @@ describe "OpenStruct#to_h" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/openstruct/to_h_spec.rb#L26 @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 Index: NEWS =================================================================== --- NEWS (revision 66495) +++ NEWS (revision 66496) @@ -239,6 +239,13 @@ sufficient information, see the ChangeLo https://github.com/ruby/ruby/blob/trunk/NEWS#L239 * 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] -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/