diff --git a/rbtree.c b/rbtree.c index 51f8238..3095cd1 100644 --- a/rbtree.c +++ b/rbtree.c @@ -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 @@ -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 diff --git a/test.rb b/test.rb index 37a7db0..dc4715a 100644 --- a/test.rb +++ b/test.rb @@ -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