Skip to content

Commit

Permalink
Restore to_hash default preservation
Browse files Browse the repository at this point in the history
Restores the functionality removed in #3 in a way that's compatible
with the newer Ruby versions.
  • Loading branch information
BrianHawley committed Jan 20, 2020
1 parent 3f65905 commit 38e7277
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rbtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#define RETURN_ENUMERATOR(obj, argc, argv) ((void)0)
#endif

#ifndef RHASH_SET_IFNONE
#define RHASH_SET_IFNONE(h, ifnone) (RHASH_IFNONE(h) = ifnone)
#endif

#if !defined(RUBY_API_VERSION_CODE) || (RUBY_API_VERSION_CODE < 20700)
#define HAVE_TAINT
#endif
Expand Down Expand Up @@ -1084,6 +1088,7 @@ rbtree_to_hash(VALUE self)

hash = rb_hash_new();
rbtree_for_each(self, to_hash_i, (void*)hash);
RHASH_SET_IFNONE(hash, IFNONE(self));
if (FL_TEST(self, RBTREE_PROC_DEFAULT))
FL_SET(hash, HASH_PROC_DEFAULT);
#ifdef HAVE_TAINT
Expand Down
9 changes: 9 additions & 0 deletions test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,15 @@ def test_to_hash
@rbtree.default = "e"
hash = @rbtree.to_hash
assert_equal(@rbtree.to_a.flatten, hash.to_a.flatten)
assert_equal("e", hash.default)

rbtree = RBTree.new { "e" }
hash = rbtree.to_hash
if (hash.respond_to?(:default_proc))
assert_equal(rbtree.default_proc, hash.default_proc)
else
assert_equal(rbtree.default_proc, hash.default)
end
end

def test_to_rbtree
Expand Down

0 comments on commit 38e7277

Please sign in to comment.