Function magnus::block::yield_values

source ·
pub fn yield_values<T, U>(vals: T) -> Result<U, Error>
where T: ArgList, U: TryConvert,
Expand description

Yields multiple values to the block given to the current method.

Note: A method using yield_values converted to an Enumerator with to_enum/Value::enumeratorize will result in a non-functional Enumerator on versions of Ruby before 3.1. See YieldValues for an alternative.

§Panics

Panics if called from a non-Ruby thread. See Ruby::yield_values for the non-panicking version.

§Examples

use magnus::{
    block::yield_values, define_global_function, function, rb_assert, Error, RArray, Value,
};

fn metasyntactic_variables() -> Result<(), Error> {
    let _: Value = yield_values((0, "foo"))?;
    let _: Value = yield_values((1, "bar"))?;
    let _: Value = yield_values((2, "baz"))?;
    Ok(())
}

define_global_function(
    "metasyntactic_variables",
    function!(metasyntactic_variables, 0),
);

let vars = RArray::new();
rb_assert!(
    "metasyntactic_variables {|pos, var| vars << [pos, var]} == nil",
    vars
);
rb_assert!(r#"vars == [[0, "foo"], [1, "bar"], [2, "baz"]]"#, vars);