summaryrefslogtreecommitdiff
path: root/thread_sync.c
diff options
context:
space:
mode:
authorChris Seaton <chris.seaton@shopify.com>2021-02-11 10:14:18 +0000
committerGitHub <noreply@github.com>2021-02-11 19:14:18 +0900
commitc3b2bb0969cc47dcfb1f624c94a46cdf1e2cc2ad (patch)
tree042fdc8b7fc52095c46cae33076d2d3f50755a8a /thread_sync.c
parenta0216b1acf375e8b3fb7dbb31bd5711acc76d05e (diff)
downloadruby-c3b2bb0969cc47dcfb1f624c94a46cdf1e2cc2ad.tar.gz
The Queue constructor should take an initial set of objects
Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Diffstat (limited to 'thread_sync.c')
-rw-r--r--thread_sync.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/thread_sync.c b/thread_sync.c
index 9932abde11..131ace2fda 100644
--- a/thread_sync.c
+++ b/thread_sync.c
@@ -839,15 +839,27 @@ queue_closed_result(VALUE self, struct rb_queue *q)
/*
* Document-method: Queue::new
*
- * Creates a new queue instance.
+ * Creates a new queue instance, optionally using the contents of an Enumerable
+ * for its initial state.
+ *
+ * Example:
+ *
+ * q = Queue.new
+ * q = Queue.new([a, b, c])
+ * q = Queue.new(items)
*/
static VALUE
-rb_queue_initialize(VALUE self)
+rb_queue_initialize(int argc, VALUE *argv, VALUE self)
{
+ VALUE initial;
struct rb_queue *q = queue_ptr(self);
RB_OBJ_WRITE(self, &q->que, ary_buf_new());
list_head_init(queue_waitq(q));
+ rb_scan_args(argc, argv, "01", &initial);
+ if (argc == 1) {
+ rb_ary_concat(q->que, rb_convert_type(initial, T_ARRAY, "Array", "to_ary"));
+ }
return self;
}
@@ -1570,7 +1582,7 @@ Init_thread_sync(void)
rb_eClosedQueueError = rb_define_class("ClosedQueueError", rb_eStopIteration);
- rb_define_method(rb_cQueue, "initialize", rb_queue_initialize, 0);
+ rb_define_method(rb_cQueue, "initialize", rb_queue_initialize, -1);
rb_undef_method(rb_cQueue, "initialize_copy");
rb_define_method(rb_cQueue, "marshal_dump", undumpable, 0);
rb_define_method(rb_cQueue, "close", rb_queue_close, 0);