Function magnus::block::yield_value

source ·
pub fn yield_value<T, U>(val: T) -> Result<U, Error>
where T: IntoValue, U: TryConvert,
Expand description

Yields a value to the block given to the current method.

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

§Panics

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

§Examples

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

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

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

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