Function magnus::define_variable

source ·
pub fn define_variable<T>(name: &str, initial: T) -> Result<*mut Value, Error>
where T: IntoValue,
Expand description

Define a global variable.

§Panics

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

§Examples

use magnus::{define_variable, prelude::*, rb_assert, RString};

let v = define_variable("example", 42).unwrap();
rb_assert!("$example == 42");

// safe as long as another thread isn't modifying v
unsafe {
    *v = RString::new("answer").as_value();
}
rb_assert!(r#"$example == "answer""#);