pub struct RBignum(/* private fields */);
Expand description
Implementations§
source§impl RBignum
impl RBignum
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(RBignum)
if val
is a RBignum
, None
otherwise.
§Examples
use magnus::{eval, RBignum};
assert!(RBignum::from_value(eval("9223372036854775807").unwrap()).is_some());
// too small
assert!(RBignum::from_value(eval("0").unwrap()).is_none());
// not an int
assert!(RBignum::from_value(eval("1.23").unwrap()).is_none());
sourcepub fn from_i64(n: i64) -> Result<Self, Fixnum>
pub fn from_i64(n: i64) -> Result<Self, Fixnum>
Create a new RBignum
from an i64.
Returns Ok(RBignum)
if n
is large enough to require a bignum,
otherwise returns Err(Fixnum)
.
§Panics
Panics if called from a non-Ruby thread. See Ruby::bignum_from_i64
for the non-panicking version.
§Examples
use magnus::RBignum;
assert!(RBignum::from_i64(4611686018427387904).is_ok());
assert!(RBignum::from_i64(-4611686018427387905).is_ok());
// too small
assert!(RBignum::from_i64(0).is_err());
sourcepub fn from_u64(n: u64) -> Result<Self, Fixnum>
pub fn from_u64(n: u64) -> Result<Self, Fixnum>
Create a new RBignum
from an u64.
Returns Ok(RBignum)
if n
is large enough to require a bignum,
otherwise returns Err(Fixnum)
.
§Panics
Panics if called from a non-Ruby thread. See Ruby::bignum_from_u64
for the non-panicking version.
§Examples
use magnus::RBignum;
assert!(RBignum::from_u64(4611686018427387904).is_ok());
// too small
assert!(RBignum::from_u64(0).is_err());
sourcepub fn to_i64(self) -> Result<i64, Error>
pub fn to_i64(self) -> Result<i64, Error>
Convert self
to an i64
. Returns Err
if self
is out of range for
i64
.
§Examples
use magnus::{eval, RBignum};
assert_eq!(
eval::<RBignum>("4611686018427387904")
.unwrap()
.to_i64()
.unwrap(),
4611686018427387904
);
assert_eq!(
eval::<RBignum>("-4611686018427387905")
.unwrap()
.to_i64()
.unwrap(),
-4611686018427387905
);
assert!(eval::<RBignum>("9223372036854775808")
.unwrap()
.to_i64()
.is_err());
assert!(eval::<RBignum>("-9223372036854775809")
.unwrap()
.to_i64()
.is_err());
sourcepub fn to_isize(self) -> Result<isize, Error>
pub fn to_isize(self) -> Result<isize, Error>
Convert self
to an isize
. Returns Err
if self
is out of range
for isize
.
§Examples
use magnus::{eval, RBignum};
assert_eq!(
eval::<RBignum>("4611686018427387904")
.unwrap()
.to_isize()
.unwrap(),
4611686018427387904
);
assert_eq!(
eval::<RBignum>("-4611686018427387905")
.unwrap()
.to_isize()
.unwrap(),
-4611686018427387905
);
sourcepub fn to_u64(self) -> Result<u64, Error>
pub fn to_u64(self) -> Result<u64, Error>
Convert self
to a u64
. Returns Err
if self
is negative or out
of range for u64
.
§Examples
use magnus::{eval, RBignum};
assert_eq!(
eval::<RBignum>("4611686018427387904")
.unwrap()
.to_u64()
.unwrap(),
4611686018427387904
);
assert!(eval::<RBignum>("18446744073709551616")
.unwrap()
.to_u64()
.is_err());
sourcepub fn to_usize(self) -> Result<usize, Error>
pub fn to_usize(self) -> Result<usize, Error>
Convert self
to a usize
. Returns Err
if self
is negative or out
of range for usize
.
§Examples
use magnus::{eval, RBignum};
assert_eq!(
eval::<RBignum>("4611686018427387904")
.unwrap()
.to_usize()
.unwrap(),
4611686018427387904
);
assert!(eval::<RBignum>("18446744073709551616")
.unwrap()
.to_usize()
.is_err());
sourcepub fn is_positive(self) -> bool
pub fn is_positive(self) -> bool
Check if self
is positive.
§Examples
use magnus::RBignum;
let num = RBignum::from_u64(4611686018427387904).unwrap();
assert!(num.is_positive());
let num = RBignum::from_i64(-4611686018427387905).unwrap();
assert!(!num.is_positive());
sourcepub fn is_negative(self) -> bool
pub fn is_negative(self) -> bool
Check if self
is negative.
§Examples
use magnus::RBignum;
let num = RBignum::from_i64(-4611686018427387905).unwrap();
assert!(num.is_negative());
let num = RBignum::from_u64(4611686018427387904).unwrap();
assert!(!num.is_negative());
Trait Implementations§
source§impl Numeric for RBignum
impl Numeric for RBignum
source§fn coerce_bin<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
fn coerce_bin<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
op
with coercion. Read moresource§fn coerce_cmp<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
fn coerce_cmp<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
op
with coercion. Read moresource§fn coerce_relop<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
fn coerce_relop<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
op
with coercion. Read moresource§impl ReprValue for RBignum
impl ReprValue for RBignum
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