Function magnus::block::yield_splat

source ·
pub fn yield_splat<T>(vals: RArray) -> Result<T, Error>
where T: TryConvert,
Expand description

Yields a Ruby Array to the block given to the current method.

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

§Panics

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

§Examples

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

fn metasyntactic_variables() -> Result<(), Error> {
    let ary = RArray::new();
    ary.push(0)?;
    ary.push("foo")?;
    let _: Value = yield_splat(ary)?;
    let ary = RArray::new();
    ary.push(1)?;
    ary.push("bar")?;
    let _: Value = yield_splat(ary)?;
    let ary = RArray::new();
    ary.push(2)?;
    ary.push("baz")?;
    let _: Value = yield_splat(ary)?;
    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);