pub struct Proc(/* private fields */);
Expand description
Implementations§
source§impl Proc
impl Proc
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(Proc)
if val
is a Proc
, None
otherwise.
§Examples
use magnus::{block::Proc, eval, Value};
let val: Value = eval("Proc.new {|a, b| a + b}").unwrap();
assert!(Proc::from_value(val).is_some());
let val: Value = eval("1 + 2").unwrap();
assert!(Proc::from_value(val).is_none());
sourcepub fn new<R>(block: fn(_: &[Value], _: Option<Proc>) -> R) -> Selfwhere
R: BlockReturn,
pub fn new<R>(block: fn(_: &[Value], _: Option<Proc>) -> R) -> Selfwhere
R: BlockReturn,
Create a new Proc
.
As block
is a function pointer, only functions and closures that do
not capture any variables are permitted. For more flexibility (at the
cost of allocating) see from_fn
.
§Panics
Panics if called from a non-Ruby thread. See Ruby::proc_new
for the
non-panicking version.
§Examples
use magnus::{block::Proc, prelude::*, rb_assert};
let proc = Proc::new(|args, _block| {
let acc = i64::try_convert(*args.get(0).unwrap())?;
let i = i64::try_convert(*args.get(1).unwrap())?;
Ok(acc + i)
});
rb_assert!("proc.call(1, 2) == 3", proc);
rb_assert!("[1, 2, 3, 4, 5].inject(&proc) == 15", proc);
sourcepub fn from_fn<F, R>(block: F) -> Self
pub fn from_fn<F, R>(block: F) -> Self
Create a new Proc
.
See also Proc::new
, which is more efficient when block
is a
function or closure that does not capture any variables.
§Panics
Panics if called from a non-Ruby thread. See Ruby::proc_from_fn
for
the non-panicking version.
§Examples
use magnus::{block::Proc, prelude::*, rb_assert};
let mut count = 0;
let proc = Proc::from_fn(move |args, _block| {
let step = i64::try_convert(*args.get(0).unwrap())?;
count += step;
Ok(count)
});
rb_assert!("proc.call(1) == 1", proc);
rb_assert!("proc.call(1) == 2", proc);
rb_assert!("proc.call(2) == 4", proc);
sourcepub fn call<A, T>(self, args: A) -> Result<T, Error>where
A: RArrayArgList,
T: TryConvert,
pub fn call<A, T>(self, args: A) -> Result<T, Error>where
A: RArrayArgList,
T: TryConvert,
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.
§Examples
use magnus::{block::Proc, eval, Integer, RArray};
let proc: Proc = eval("Proc.new {|a, b| a + b}").unwrap();
// call with a tuple
let result: i64 = proc.call((1, 2)).unwrap();
assert_eq!(3, result);
// call with a slice
let result: i64 = proc
.call(&[Integer::from_i64(3), Integer::from_i64(4)][..])
.unwrap();
assert_eq!(7, result);
// call with an array
let result: i64 = proc
.call([Integer::from_i64(5), Integer::from_i64(6)])
.unwrap();
assert_eq!(11, result);
// call with a Ruby array
let array = RArray::from_vec(vec![7, 8]);
let result: i64 = proc.call(array).unwrap();
assert_eq!(15, result);
With keyword arguments:
use magnus::{block::Proc, eval, kwargs};
let proc: Proc = eval("Proc.new {|a, b:, c:| a + b + c}").unwrap();
let result: i64 = proc.call((1, kwargs!("b" => 2, "c" => 3))).unwrap();
assert_eq!(6, result);
Ignoring return value:
use magnus::{block::Proc, eval, rb_assert, Value};
let proc: Proc = eval("Proc.new { $called = true }").unwrap();
let _: Value = proc.call(()).unwrap();
rb_assert!("$called == true");
sourcepub fn arity(self) -> i64
pub fn arity(self) -> i64
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: Proc = eval("proc {nil}").unwrap();
assert_eq!(proc.arity(), 0);
let proc: Proc = eval("proc {|a| a + 1}").unwrap();
assert_eq!(proc.arity(), 1);
let proc: Proc = eval("proc {|a, b| a + b}").unwrap();
assert_eq!(proc.arity(), 2);
let proc: Proc = eval("proc {|*args| args.sum}").unwrap();
assert_eq!(proc.arity(), -1);
Trait Implementations§
source§impl Object for Proc
impl Object for Proc
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 moresource§impl ReprValue for Proc
impl ReprValue for Proc
source§fn equal<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
fn equal<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
#==
. Read moresource§fn eql<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
fn eql<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
#eql?
. Read moresource§fn hash(self) -> Result<Integer, Error>
fn hash(self) -> Result<Integer, Error>
self
. Read moresource§fn to_bool(self) -> bool
fn to_bool(self) -> bool
self
to a bool
, following Ruby’s rules of false
and nil
as boolean false
and everything else boolean true
. Read moresource§fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
source§fn block_call<M, A, R, T>(
self,
method: M,
args: A,
block: fn(_: &[Value], _: Option<Proc>) -> R
) -> Result<T, Error>
fn block_call<M, A, R, T>( self, method: M, args: A, block: fn(_: &[Value], _: Option<Proc>) -> R ) -> Result<T, Error>
source§fn respond_to<M>(self, method: M, include_private: bool) -> Result<bool, Error>where
M: IntoId,
fn respond_to<M>(self, method: M, include_private: bool) -> Result<bool, Error>where
M: IntoId,
self
responds to the given Ruby method. Read more