Skip to content

Commit

Permalink
Refactor {Hash, Array, Enumerable, Struct , ENV}#to_h methods and ext…
Browse files Browse the repository at this point in the history
…ract processing of returned by a block value into a helper method
  • Loading branch information
andrykonchin committed Jul 1, 2024
1 parent 85445a7 commit b4faebd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/array/to_h_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
[:a, :b].to_h { |k| [k, k.to_s] }.should == { a: 'a', b: 'b' }
end

it "passes to a block each element as a single argument" do
ScratchPad.record []
[[:a, 1], [:b, 2]].to_h { |*args| ScratchPad << args; [args[0], args[1]] }
ScratchPad.recorded.sort.should == [[[:a, 1]], [[:b, 2]]]
end

it "raises ArgumentError if block returns longer or shorter array" do
-> do
[:a, :b].to_h { |k| [k, k.to_s, 1] }
Expand Down
8 changes: 8 additions & 0 deletions core/enumerable/to_h_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ def enum.each(*args)
@enum.to_h { |k| [k, k.to_s] }.should == { a: 'a', b: 'b' }
end

it "passes to a block each element as a single argument" do
enum_of_arrays = EnumerableSpecs::EachDefiner.new([:a, 1], [:b, 2])

ScratchPad.record []
enum_of_arrays.to_h { |*args| ScratchPad << args; [args[0], args[1]] }
ScratchPad.recorded.sort.should == [[[:a, 1]], [[:b, 2]]]
end

it "raises ArgumentError if block returns longer or shorter array" do
-> do
@enum.to_h { |k| [k, k.to_s, 1] }
Expand Down
12 changes: 12 additions & 0 deletions core/env/to_h_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
ENV.to_h { |k, v| [k, v.upcase] }.should == { 'a' => "B", 'c' => "D" }
end

it "passes to a block each pair's key and value as separate arguments" do
ENV.replace("a" => "b", "c" => "d")

ScratchPad.record []
ENV.to_h { |k, v| ScratchPad << [k, v]; [k, v] }
ScratchPad.recorded.sort.should == [["a", "b"], ["c", "d"]]

ScratchPad.record []
ENV.to_h { |*args| ScratchPad << args; [args[0], args[1]] }
ScratchPad.recorded.sort.should == [["a", "b"], ["c", "d"]]
end

it "does not require the array elements to be strings" do
ENV.replace("a" => "b", "c" => "d")
ENV.to_h { |k, v| [k.to_sym, v.to_sym] }.should == { :a => :b, :c => :d }
Expand Down
12 changes: 12 additions & 0 deletions core/struct/to_h_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
h.should == { "make" => "ford", "model" => "ranger", "year" => "" }
end

it "passes to a block each pair's key and value as separate arguments" do
s = StructClasses::Ruby.new('3.2.4', 'macos')

ScratchPad.record []
s.to_h { |k, v| ScratchPad << [k, v]; [k, v] }
ScratchPad.recorded.sort.should == [[:platform, 'macos'], [:version, '3.2.4']]

ScratchPad.record []
s.to_h { |*args| ScratchPad << args; [args[0], args[1]] }
ScratchPad.recorded.sort.should == [[:platform, 'macos'], [:version, '3.2.4']]
end

it "raises ArgumentError if block returns longer or shorter array" do
-> do
StructClasses::Car.new.to_h { |k, v| [k.to_s, "#{v}".downcase, 1] }
Expand Down

0 comments on commit b4faebd

Please sign in to comment.