#[repr(transparent)]pub struct Binding(_);
Expand description
Implementations§
source§impl Binding
impl Binding
sourcepub fn new() -> Self
👎Deprecated since 0.2.0: this will no longer function as of Ruby 3.2Available on ruby_lte_3_1
only.
pub fn new() -> Self
ruby_lte_3_1
only.Create a new Binding
from the current Ruby execution context.
Examples
use magnus::Binding;
let binding = Binding::new();
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(Binding)
if val
is a Binding
, None
otherwise.
sourcepub fn eval<T, U>(&self, s: T) -> Result<U, Error>where
T: Into<RString>,
U: TryConvert,
pub fn eval<T, U>(&self, s: T) -> Result<U, Error>where
T: Into<RString>,
U: TryConvert,
Evaluate a string of Ruby code within the binding’s context.
Examples
use magnus::{eval, Binding};
let binding = eval::<Binding>("binding").unwrap();
assert_eq!(binding.eval::<_, i64>("1 + 2").unwrap(), 3);
sourcepub fn local_variable_get<N, T>(&self, name: N) -> Result<T, Error>where
N: Into<Symbol>,
T: TryConvert,
pub fn local_variable_get<N, T>(&self, name: N) -> Result<T, Error>where
N: Into<Symbol>,
T: TryConvert,
Get the named local variable from the binding.
Returns Ok(T)
if the method returns without error and the return
value converts to a T
, or returns Err
if the local variable does
not exist or the conversion fails.
Examples
use magnus::{eval, Binding, Value};
let binding = eval::<Binding>("binding").unwrap();
binding.local_variable_set("a", 1);
assert_eq!(binding.local_variable_get::<_, i64>("a").unwrap(), 1);
assert!(binding.local_variable_get::<_, Value>("b").is_err());
sourcepub fn local_variable_set<N, T>(&self, name: N, val: T)where
N: Into<Symbol>,
T: Into<Value>,
pub fn local_variable_set<N, T>(&self, name: N, val: T)where
N: Into<Symbol>,
T: Into<Value>,
Set the named local variable in the binding.
Examples
use magnus::{eval, Binding};
let binding = eval::<Binding>("binding").unwrap();
binding.local_variable_set("a", 1);
assert_eq!(binding.local_variable_get::<_, i64>("a").unwrap(), 1);
Methods from Deref<Target = Value>§
sourcepub unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>
pub unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>
Convert self
to a Rust string.
Safety
This may return a direct view of memory owned and managed by Ruby. Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.
This can be used safely by immediately calling
into_owned
on the return value.
Examples
use magnus::{eval, QTRUE};
let value = QTRUE;
// safe as we neve give Ruby a chance to free the string.
let s = unsafe { value.to_s() }.unwrap().into_owned();
assert_eq!(s, "true");
sourcepub unsafe fn classname(&self) -> Cow<'_, str>
pub unsafe fn classname(&self) -> Cow<'_, str>
Return the name of self
’s class.
Safety
Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.
This can be used safely by immediately calling
into_owned
on the return value.
Examples
use magnus::{eval, RHash};
let value = RHash::new();
// safe as we never give Ruby a chance to free the string.
let s = unsafe { value.classname() }.into_owned();
assert_eq!(s, "Hash");
Trait Implementations§
source§impl Object for Binding
impl Object for Binding
source§fn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
fn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
self
’s scope. Read more