Project

General

Profile

Feature #15451 » ostruct-to_h-with-block.patch

shuujii (Shuji KOBAYASHI), 12/22/2018 01:14 PM

View differences:

NEWS
* 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]
lib/ostruct.rb
@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
if block_given?
@table.to_h{|key, value| yield key, value}
else
@table.dup
end
end
#
spec/ruby/library/openstruct/to_h_spec.rb
@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
test/ostruct/test_ostruct.rb
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)
    (1-1/1)