Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Set#to_a(&) #14519

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spec/std/set_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ describe "Set" do
Set{1, 2, 3}.to_a.should eq([1, 2, 3])
end

it "does support giving a block to to_a" do
Set{1, 2, 3}.to_a { |x| x + 1 }.should eq([2, 3, 4])
end

it "does to_s" do
Set{1, 2, 3}.to_s.should eq("Set{1, 2, 3}")
Set{"foo"}.to_s.should eq(%(Set{"foo"}))
Expand Down
4 changes: 2 additions & 2 deletions src/set.cr
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,12 @@ struct Set(T)
# Returns an `Array` with the results of running *block* against each element of the collection.
#
# ```
# Set{1, 2, 3, 4, 5}.to_a { |i| i // 2 } # => [0, 1, 2]
# Set{1, 2, 3, 4, 5}.to_a { |i| i // 2 } # => [0, 1, 1, 2, 2]
# ```
def to_a(& : T -> U) : Array(U) forall U
array = Array(U).new(size)
@hash.each_key do |key|
array << key
array << yield key
end
array
end
Expand Down
Loading