Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix Set#to_h to be backwards compatible when called with a block #13248

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

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 11 additions & 0 deletions set.c
Original file line number Diff line number Diff line change
Expand Up @@ -1858,9 +1858,20 @@ set_to_hash_i(st_data_t key, st_data_t arg)
return ST_CONTINUE;
}

/*
* call-seq:
* to_h -> hash
* to_h {|element| ...} -> hash
*
* Without a block, returns a hash where the set's elements
* are keys, and all values are true. With a block, calls
* super (Enumerable#to_h).
*/
static VALUE
set_i_to_h(VALUE set)
{
if (rb_block_given_p()) return rb_call_super(0, NULL);

st_index_t size = RSET_SIZE(set);
VALUE hash;
if (RSET_COMPARE_BY_IDENTITY(set)) {
Expand Down
6 changes: 6 additions & 0 deletions test/ruby/test_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ def test_to_a
assert_equal([1,2,3], ary.sort)
end

def test_to_h
set = Set[1,2]
assert_equal({1 => true, 2 => true}, set.to_h)
assert_equal({1 => false, 2 => false}, set.to_h { [it, false] })
end

def test_flatten
# test1
set1 = Set[
Expand Down
Loading