File: yield.rs

package info (click to toggle)
rust-magnus 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,468 kB
  • sloc: ruby: 150; sh: 17; makefile: 2
file content (34 lines) | stat: -rw-r--r-- 792 bytes parent folder | download
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
use magnus::{eval, method, rb_assert, Error, Ruby, Value};

fn flipflop(ruby: &Ruby, _rb_self: Value, mut val: bool) -> Result<(), Error> {
    val = ruby.yield_value(val)?;
    loop {
        val = ruby.yield_value(!val)?;
    }
}

#[test]
fn it_yields() {
    let ruby = unsafe { magnus::embed::init() };

    ruby.define_global_function("flipflop", method!(flipflop, 1));

    let values = ruby.ary_new();
    let i: Value = eval!(
        ruby,
        "
        i = 0
        flipflop(true) do |val|
          values << val
          i += 1 if val
          break if i > 5
          val
        end
        i
        ",
        values
    )
    .unwrap();

    rb_assert!(ruby, "i == 6 && values == [true, false, true, false, true, false, true, false, true, false, true]", i, values);
}