mrubyedge 1.1.12

mruby/edge is yet another mruby that is specialized for running on WASM
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
use std::{cell::Cell, rc::Rc};

use crate::{
    Error,
    yamrb::{
        helpers::{mrb_call_block, mrb_define_module_cmethod, mrb_funcall},
        value::{RFn, RObject, RProc},
        vm::VM,
    },
};

pub(crate) fn initialize_enumerable(vm: &mut VM) {
    let enumerable_module = vm.define_module("Enumerable", None);

    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "map",
        Box::new(mrb_enumerable_map),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "find",
        Box::new(mrb_enumerable_find),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "select",
        Box::new(mrb_enumerable_select),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "all?",
        Box::new(mrb_enumerable_all),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "any?",
        Box::new(mrb_enumerable_any),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "delete_if",
        Box::new(mrb_enumerable_delete_if),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "each_with_index",
        Box::new(mrb_enumerable_each_with_index),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "sort",
        Box::new(mrb_enumerable_sort),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "sort_by",
        Box::new(mrb_enumerable_sort_by),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "max",
        Box::new(mrb_enumerable_max),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "min",
        Box::new(mrb_enumerable_min),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "minmax",
        Box::new(mrb_enumerable_minmax),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "compact",
        Box::new(mrb_enumerable_compact),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "count",
        Box::new(mrb_enumerable_count),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "to_a",
        Box::new(mrb_enumerable_to_a),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "uniq",
        Box::new(mrb_enumerable_uniq),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "reduce",
        Box::new(mrb_enumerable_reduce),
    );
    mrb_define_module_cmethod(
        vm,
        enumerable_module.clone(),
        "sum",
        Box::new(mrb_enumerable_sum),
    );
}

fn rproc_from_rust_block(vm: &mut VM, rfn: RFn) -> Result<Rc<RObject>, Error> {
    vm.push_fnblock(Rc::new(rfn))?;
    let block = RProc {
        is_rb_func: false,
        is_fnblock: true,
        sym_id: None,
        next: None,
        irep: None,
        func: None,
        environ: None,
        block_self: vm.getself().ok(),
    };
    Ok(RObject::proc(block).to_refcount_assigned())
}

// Enumerable#to_a: Returns an array containing all elements
fn mrb_enumerable_to_a(vm: &mut VM, _args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let results: Rc<RObject> = RObject::array(vec![]).to_refcount_assigned();
    let results_ref = results.clone();
    let wrapping_block: RFn = Box::new(move |vm: &mut VM, args: &[Rc<RObject>]| {
        mrb_funcall(
            vm,
            Some(results_ref.clone()),
            "push",
            std::slice::from_ref(&args[0]),
        )?;
        Ok(Rc::new(RObject::nil()))
    });

    let this = vm.getself()?;
    let block = rproc_from_rust_block(vm, wrapping_block)?;
    mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
    vm.pop_fnblock()?;

    Ok(results)
}

// Enumerable#map: Returns a new array with the results of running block once for every element
fn mrb_enumerable_map(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let original_block = args
        .last()
        .cloned()
        .ok_or_else(|| Error::ArgumentError("block should be specified".to_string()))?;
    let results: Rc<RObject> = RObject::array(vec![]).to_refcount_assigned();
    let results_ref = results.clone();
    let wrapping_block: RFn = Box::new(move |vm: &mut VM, args: &[Rc<RObject>]| {
        let block = original_block.clone();
        let result = mrb_call_block(vm, block, None, args, 0)?;
        mrb_funcall(
            vm,
            Some(results_ref.clone()),
            "push",
            std::slice::from_ref(&result),
        )?;
        Ok(result)
    });

    let this = vm.getself()?;
    let block = rproc_from_rust_block(vm, wrapping_block)?;
    mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
    vm.pop_fnblock()?;

    Ok(results)
}

// Enumerable#find: Returns the first element for which the block returns true
fn mrb_enumerable_find(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let original_block = args
        .last()
        .cloned()
        .ok_or_else(|| Error::ArgumentError("block should be specified".to_string()))?;
    let found = Cell::new(false);
    let result_box: Rc<RObject> = RObject::array(vec![]).to_refcount_assigned();
    let result_box_ref = result_box.clone();
    let wrapping_block: RFn = Box::new(move |vm: &mut VM, args: &[Rc<RObject>]| {
        if found.get() {
            return Ok(Rc::new(RObject::nil()));
        }

        let block = original_block.clone();
        let result = mrb_call_block(vm, block, None, args, 0)?;
        if result.is_truthy() {
            mrb_funcall(
                vm,
                Some(result_box_ref.clone()),
                "push",
                std::slice::from_ref(&args[0]),
            )?;
            found.set(true);
        }
        Ok(result)
    });
    let this = vm.getself()?;
    let block = rproc_from_rust_block(vm, wrapping_block)?;
    mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
    vm.pop_fnblock()?;

    let found = mrb_funcall(vm, result_box.into(), "pop", &[])?;
    Ok(found)
}

// Enumerable#select: Returns a new array containing all elements for which the block returns true
fn mrb_enumerable_select(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let original_block = args
        .last()
        .cloned()
        .ok_or_else(|| Error::ArgumentError("block should be specified".to_string()))?;
    let results: Rc<RObject> = RObject::array(vec![]).to_refcount_assigned();
    let results_ref = results.clone();
    let wrapping_block: RFn = Box::new(move |vm: &mut VM, args: &[Rc<RObject>]| {
        let block = original_block.clone();
        let result = mrb_call_block(vm, block, None, args, 0)?;
        if result.is_truthy() {
            mrb_funcall(
                vm,
                Some(results_ref.clone()),
                "push",
                std::slice::from_ref(&args[0]),
            )?;
        }
        Ok(result)
    });

    let this = vm.getself()?;
    let block = rproc_from_rust_block(vm, wrapping_block)?;
    mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
    vm.pop_fnblock()?;

    Ok(results)
}

// Enumerable#all?: Returns true if all elements match the condition
fn mrb_enumerable_all(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let original_block = args
        .last()
        .cloned()
        .ok_or_else(|| Error::ArgumentError("block should be specified".to_string()))?;
    let all_true = Rc::new(Cell::new(true));
    let all_true_ref = all_true.clone();
    let wrapping_block: RFn = Box::new(move |vm: &mut VM, args: &[Rc<RObject>]| {
        if !all_true_ref.get() {
            return Ok(Rc::new(RObject::nil()));
        }

        let block = original_block.clone();
        let result = mrb_call_block(vm, block, None, args, 0)?;
        if !result.is_truthy() {
            all_true_ref.set(false);
        }
        Ok(result)
    });

    let this = vm.getself()?;
    let block = rproc_from_rust_block(vm, wrapping_block)?;
    mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
    vm.pop_fnblock()?;

    Ok(Rc::new(RObject::boolean(all_true.get())))
}

// Enumerable#any?: Returns true if any element matches the condition
fn mrb_enumerable_any(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let original_block = args
        .last()
        .cloned()
        .ok_or_else(|| Error::ArgumentError("block should be specified".to_string()))?;
    let found_true = Rc::new(Cell::new(false));
    let found_true_ref = found_true.clone();
    let wrapping_block: RFn = Box::new(move |vm: &mut VM, args: &[Rc<RObject>]| {
        if found_true_ref.get() {
            return Ok(Rc::new(RObject::nil()));
        }

        let block = original_block.clone();
        let result = mrb_call_block(vm, block, None, args, 0)?;
        if result.is_truthy() {
            found_true_ref.set(true);
        }
        Ok(result)
    });

    let this = vm.getself()?;
    let block = rproc_from_rust_block(vm, wrapping_block)?;
    mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
    vm.pop_fnblock()?;

    Ok(Rc::new(RObject::boolean(found_true.get())))
}

// Enumerable#delete_if: Deletes every element for which block evaluates to true
fn mrb_enumerable_delete_if(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let original_block = args
        .last()
        .cloned()
        .ok_or_else(|| Error::ArgumentError("block should be specified".to_string()))?;
    let results: Rc<RObject> = RObject::array(vec![]).to_refcount_assigned();
    let results_ref = results.clone();
    let wrapping_block: RFn = Box::new(move |vm: &mut VM, args: &[Rc<RObject>]| {
        let block = original_block.clone();
        let result = mrb_call_block(vm, block, None, args, 0)?;
        if !result.is_truthy() {
            mrb_funcall(
                vm,
                Some(results_ref.clone()),
                "push",
                std::slice::from_ref(&args[0]),
            )?;
        }
        Ok(result)
    });

    let this = vm.getself()?;
    let block = rproc_from_rust_block(vm, wrapping_block)?;
    mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
    vm.pop_fnblock()?;

    Ok(results)
}

// Enumerable#each_with_index: Calls block with two arguments, the item and its index
fn mrb_enumerable_each_with_index(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let original_block = args
        .last()
        .cloned()
        .ok_or_else(|| Error::ArgumentError("block should be specified".to_string()))?;
    let index = Rc::new(Cell::new(0i64));
    let index_ref = index.clone();
    let wrapping_block: RFn = Box::new(move |vm: &mut VM, args: &[Rc<RObject>]| {
        let block = original_block.clone();
        let idx = index_ref.get();
        let index_obj = Rc::new(RObject::integer(idx));
        let block_args = vec![args[0].clone(), index_obj];
        let result = mrb_call_block(vm, block, None, &block_args, 0)?;
        index_ref.set(idx + 1);
        Ok(result)
    });

    let this = vm.getself()?;
    let block = rproc_from_rust_block(vm, wrapping_block)?;
    mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
    vm.pop_fnblock()?;

    Ok(this)
}

// Enumerable#sort: Returns an array with sorted elements
fn mrb_enumerable_sort(vm: &mut VM, _args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let this = vm.getself()?;
    let array = mrb_funcall(vm, Some(this), "to_a", &[])?;
    let mut collected: Vec<Rc<RObject>> = array.as_ref().try_into()?;

    collected.sort_by(|a, b| {
        let args = vec![b.clone()];
        let cmp_result = mrb_funcall(vm, Some(a.clone()), "<=>", &args);
        match cmp_result {
            Ok(cmp_obj) => {
                let cmp_val: Result<i64, _> = cmp_obj.as_ref().try_into();
                match cmp_val {
                    Ok(v) => v.cmp(&0),
                    Err(_) => std::cmp::Ordering::Equal,
                }
            }
            Err(_) => std::cmp::Ordering::Equal,
        }
    });

    Ok(RObject::array(collected).to_refcount_assigned())
}

// Enumerable#sort_by: Returns an array with elements sorted by the block's return value
fn mrb_enumerable_sort_by(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let original_block = args
        .last()
        .cloned()
        .ok_or_else(|| Error::ArgumentError("block should be specified".to_string()))?;

    // Collect elements first using to_a
    let this = vm.getself()?;
    let array = mrb_funcall(vm, Some(this), "to_a", &[])?;
    let elements: Vec<Rc<RObject>> = array.as_ref().try_into()?;

    // Collect keys by calling the block on each element
    let mut sort_keys: Vec<Rc<RObject>> = Vec::new();
    for elem in &elements {
        let key = mrb_call_block(
            vm,
            original_block.clone(),
            None,
            std::slice::from_ref(elem),
            0,
        )?;
        sort_keys.push(key);
    }

    let mut elements = elements;
    let mut sort_keys = sort_keys;

    let mut pairs: Vec<(Rc<RObject>, Rc<RObject>)> =
        elements.drain(..).zip(sort_keys.drain(..)).collect();

    pairs.sort_by(|a, b| {
        let args = vec![b.1.clone()];
        let cmp_result = mrb_funcall(vm, Some(a.1.clone()), "<=>", &args);
        match cmp_result {
            Ok(cmp_obj) => {
                let cmp_val: Result<i64, _> = cmp_obj.as_ref().try_into();
                match cmp_val {
                    Ok(v) => v.cmp(&0),
                    Err(_) => std::cmp::Ordering::Equal,
                }
            }
            Err(_) => std::cmp::Ordering::Equal,
        }
    });

    let sorted: Vec<Rc<RObject>> = pairs.into_iter().map(|(elem, _)| elem).collect();
    Ok(RObject::array(sorted).to_refcount_assigned())
}

// Enumerable#max: Returns the maximum element
fn mrb_enumerable_max(vm: &mut VM, _args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let this = vm.getself()?;
    let array = mrb_funcall(vm, Some(this), "to_a", &[])?;
    let collected: Vec<Rc<RObject>> = array.as_ref().try_into()?;

    if collected.is_empty() {
        return Ok(Rc::new(RObject::nil()));
    }

    let mut max = collected[0].clone();
    for elem in collected.iter().skip(1) {
        let args = vec![elem.clone()];
        let cmp: i64 = mrb_funcall(vm, Some(max.clone()), "<=>", &args)?
            .as_ref()
            .try_into()?;
        if cmp < 0 {
            max = elem.clone();
        }
    }
    Ok(max)
}

// Enumerable#min: Returns the minimum element
fn mrb_enumerable_min(vm: &mut VM, _args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let this = vm.getself()?;
    let array = mrb_funcall(vm, Some(this), "to_a", &[])?;
    let collected: Vec<Rc<RObject>> = array.as_ref().try_into()?;

    if collected.is_empty() {
        return Ok(Rc::new(RObject::nil()));
    }

    let mut min = collected[0].clone();
    for elem in collected.iter().skip(1) {
        let args = vec![elem.clone()];
        let cmp: i64 = mrb_funcall(vm, Some(min.clone()), "<=>", &args)?
            .as_ref()
            .try_into()?;
        if cmp > 0 {
            min = elem.clone();
        }
    }
    Ok(min)
}

// Enumerable#minmax: Returns a two-element array containing the minimum and maximum
fn mrb_enumerable_minmax(vm: &mut VM, _args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let this = vm.getself()?;
    let array = mrb_funcall(vm, Some(this), "to_a", &[])?;
    let collected: Vec<Rc<RObject>> = array.as_ref().try_into()?;

    if collected.is_empty() {
        return Ok(
            RObject::array(vec![Rc::new(RObject::nil()), Rc::new(RObject::nil())])
                .to_refcount_assigned(),
        );
    }

    let mut min = collected[0].clone();
    let mut max = collected[0].clone();

    for elem in collected.iter().skip(1) {
        let args = vec![elem.clone()];
        let cmp_min: i64 = mrb_funcall(vm, Some(min.clone()), "<=>", &args)?
            .as_ref()
            .try_into()?;
        if cmp_min > 0 {
            min = elem.clone();
        }

        let args = vec![elem.clone()];
        let cmp_max: i64 = mrb_funcall(vm, Some(max.clone()), "<=>", &args)?
            .as_ref()
            .try_into()?;
        if cmp_max < 0 {
            max = elem.clone();
        }
    }

    Ok(RObject::array(vec![min, max]).to_refcount_assigned())
}

// Enumerable#compact: Returns a new array with nil values removed
fn mrb_enumerable_compact(vm: &mut VM, _args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let results: Rc<RObject> = RObject::array(vec![]).to_refcount_assigned();
    let results_ref = results.clone();
    let wrapping_block: RFn = Box::new(move |vm: &mut VM, args: &[Rc<RObject>]| {
        if !args[0].is_nil() {
            mrb_funcall(
                vm,
                Some(results_ref.clone()),
                "push",
                std::slice::from_ref(&args[0]),
            )?;
        }
        Ok(Rc::new(RObject::nil()))
    });

    let this = vm.getself()?;
    let block = rproc_from_rust_block(vm, wrapping_block)?;
    mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
    vm.pop_fnblock()?;

    Ok(results)
}

// Enumerable#count: Returns the number of elements (with optional condition)
fn mrb_enumerable_count(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let count = Rc::new(Cell::new(0i64));

    if args.is_empty() {
        // Count all elements
        let count_ref = count.clone();
        let wrapping_block: RFn = Box::new(move |_vm: &mut VM, _args: &[Rc<RObject>]| {
            count_ref.set(count_ref.get() + 1);
            Ok(Rc::new(RObject::nil()))
        });

        let this = vm.getself()?;
        let block = rproc_from_rust_block(vm, wrapping_block)?;
        mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
        vm.pop_fnblock()?;
    } else {
        // Count elements matching the block condition
        let count_ref = count.clone();
        let original_block = args.last().cloned().unwrap();
        let wrapping_block: RFn = Box::new(move |vm: &mut VM, args: &[Rc<RObject>]| {
            let block = original_block.clone();
            let result = mrb_call_block(vm, block, None, args, 0)?;
            if result.is_truthy() {
                count_ref.set(count_ref.get() + 1);
            }
            Ok(result)
        });

        let this = vm.getself()?;
        let block = rproc_from_rust_block(vm, wrapping_block)?;
        mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
        vm.pop_fnblock()?;
    }

    Ok(Rc::new(RObject::integer(count.get())))
}

// Enumerable#uniq: Returns a new array with duplicate values removed
fn mrb_enumerable_uniq(vm: &mut VM, _args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    let this = vm.getself()?;
    let array = mrb_funcall(vm, Some(this), "to_a", &[])?;
    let collected: Vec<Rc<RObject>> = array.as_ref().try_into()?;

    let mut result = Vec::new();
    for elem in collected.iter() {
        let elem_eq = elem.as_eq_value();
        if !result
            .iter()
            .any(|e: &Rc<RObject>| e.as_eq_value() == elem_eq)
        {
            result.push(elem.clone());
        }
    }
    Ok(Rc::new(RObject::array(result)))
}

// Enumerable#reduce: Combines all elements by applying a binary operation
fn mrb_enumerable_reduce(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    // Check if we have an initial value or just a block
    let (initial_value, original_block) = if args.len() == 2 {
        // Initial value provided: reduce(initial) { |acc, elem| ... }
        (Some(args[0].clone()), args[1].clone())
    } else if args.len() == 1 {
        // No initial value: reduce { |acc, elem| ... }
        (None, args[0].clone())
    } else {
        return Err(Error::ArgumentError(
            "wrong number of arguments".to_string(),
        ));
    };

    let accumulator: Rc<RObject> = if let Some(init) = initial_value {
        RObject::array(vec![init]).to_refcount_assigned()
    } else {
        RObject::array(vec![]).to_refcount_assigned()
    };

    let acc_ref = accumulator.clone();
    let wrapping_block: RFn = Box::new(move |vm: &mut VM, args: &[Rc<RObject>]| {
        let current_elem = args[0].clone();
        let acc_array: Vec<Rc<RObject>> = acc_ref.as_ref().try_into()?;

        if acc_array.is_empty() {
            // First element becomes the initial accumulator
            mrb_funcall(
                vm,
                Some(acc_ref.clone()),
                "push",
                std::slice::from_ref(&current_elem),
            )?;
        } else {
            // Call block with (accumulator, element)
            let current_acc = acc_array[0].clone();
            let block = original_block.clone();
            let result = mrb_call_block(vm, block, None, &[current_acc, current_elem], 0)?;

            // Update accumulator
            mrb_funcall(vm, Some(acc_ref.clone()), "pop", &[])?;
            mrb_funcall(
                vm,
                Some(acc_ref.clone()),
                "push",
                std::slice::from_ref(&result),
            )?;
        }
        Ok(Rc::new(RObject::nil()))
    });

    let this = vm.getself()?;
    let block = rproc_from_rust_block(vm, wrapping_block)?;
    mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
    vm.pop_fnblock()?;

    // Return the final accumulator value
    let result = mrb_funcall(vm, Some(accumulator), "first", &[])?;
    Ok(result)
}

// Enumerable#sum: Returns the sum of all elements
fn mrb_enumerable_sum(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
    // Check if we have an initial value
    let initial_value = if args.is_empty() || args[0].is_nil() {
        // Default initial value is 0
        Rc::new(RObject::integer(0))
    } else {
        // Initial value provided: sum(init)
        args[0].clone()
    };

    let accumulator: Rc<RObject> = RObject::array(vec![initial_value]).to_refcount_assigned();
    let acc_ref = accumulator.clone();

    let wrapping_block: RFn = Box::new(move |vm: &mut VM, args: &[Rc<RObject>]| {
        let current_elem = args[0].clone();
        let acc_array: Vec<Rc<RObject>> = acc_ref.as_ref().try_into()?;
        let current_acc = acc_array[0].clone();

        // Call + operator on accumulator with current element
        let result = mrb_funcall(vm, Some(current_acc), "+", &[current_elem])?;

        // Update accumulator
        mrb_funcall(vm, Some(acc_ref.clone()), "pop", &[])?;
        mrb_funcall(
            vm,
            Some(acc_ref.clone()),
            "push",
            std::slice::from_ref(&result),
        )?;
        Ok(Rc::new(RObject::nil()))
    });

    let this = vm.getself()?;
    let block = rproc_from_rust_block(vm, wrapping_block)?;
    mrb_funcall(vm, Some(this.clone()), "each", &[block])?;
    vm.pop_fnblock()?;

    // Return the final accumulator value
    let result = mrb_funcall(vm, Some(accumulator), "first", &[])?;
    Ok(result)
}