#[repr(transparent)]
pub struct Proc(_);
Expand description

Wrapper type for a Value known to be an instance of Ruby’s Proc class.

All Value methods should be available on this type through Deref, but some may be missed by this documentation.

Implementations

Return Some(Proc) if val is a Proc, None otherwise.

Create a new Proc.

Examples
use magnus::{block::Proc, eval};

let proc = Proc::new(|args, _block| {
    let acc = args.get(0).unwrap().try_convert::<i64>()?;
    let i = args.get(1).unwrap().try_convert::<i64>()?;
    Ok(acc + i)
});

let res: bool = eval!("proc.call(1, 2) == 3", proc).unwrap();
assert!(res);

let res: bool = eval!("[1, 2, 3, 4, 5].inject(&proc) == 15", proc).unwrap();
assert!(res);

Call the proc with args.

Returns Ok(T) if the proc runs without error and the return value converts into a T, or returns Err if the proc raises or the conversion fails.

Returns the number of arguments self takes.

If self takes no arguments, returns 0. If self takes only required arguments, returns the number of required arguments. If self is a lambda and has optional arguments, or is not a lambda and takes a splat argument, returns -n-1, where n is the number of required arguments. If self is not a lambda, and takes a finite number of optional arguments, returns the number of required arguments. Keyword arguments are considered as a single additional argument, that argument being required if any keyword argument is required.

Examples
use magnus::{block::Proc, eval};

let proc = eval::<Proc>("proc {nil}", ).unwrap();
assert_eq!(proc.arity(), 0);

let proc = eval::<Proc>("proc {|a| a + 1}", ).unwrap();
assert_eq!(proc.arity(), 1);

let proc = eval::<Proc>("proc {|a, b| a + b}", ).unwrap();
assert_eq!(proc.arity(), 2);

let proc = eval::<Proc>("proc {|*args| args.sum}", ).unwrap();
assert_eq!(proc.arity(), -1);

Returns whether or not self is a lambda.

Examples
use magnus::{block::Proc, eval};

let proc = eval::<Proc>("proc {|a, b| a + b}", ).unwrap();
assert!(!proc.is_lambda());

let proc = eval::<Proc>("lambda {|a, b| a + b}", ).unwrap();
assert!(proc.is_lambda());

Methods from Deref<Target = Value>

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");

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 neve give Ruby a chance to free the string.
let s = unsafe { value.classname() }.into_owned();
assert_eq!(s, "Hash");

Convert self to the Rust type T.

See the types that TryConvert is implemented on for what this method can convert to.

Examples
use magnus::{eval, Value};

assert_eq!(eval::<Value>("42").unwrap().try_convert::<i64>().unwrap(), 42);
assert_eq!(eval::<Value>("1.23").unwrap().try_convert::<i64>().unwrap(), 1);
assert_eq!(eval::<Value>("1").unwrap().try_convert::<f64>().unwrap(), 1.0);
assert_eq!(eval::<Value>("nil").unwrap().try_convert::<Option<i64>>().unwrap(), None);
assert_eq!(eval::<Value>("42").unwrap().try_convert::<Option<i64>>().unwrap(), Some(42));

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Convert val into Self.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.