pub struct Range(/* private fields */);
Expand description
Implementations§
source§impl Range
impl Range
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(Range)
if val
is an Range
, None
otherwise.
§Examples
use magnus::eval;
assert!(magnus::Range::from_value(eval("2..7").unwrap()).is_some());
assert!(magnus::Range::from_value(eval("1").unwrap()).is_none());
sourcepub fn new<T, U>(beg: T, end: U, excl: bool) -> Result<Self, Error>
pub fn new<T, U>(beg: T, end: U, excl: bool) -> Result<Self, Error>
Create a new Range
.
Returns Err
if beg
and end
are not comparable.
§Panics
Panics if called from a non-Ruby thread. See Ruby::range_new
for
the non-panicking version.
§Examples
use magnus::rb_assert;
let range = magnus::Range::new(2, 7, false).unwrap();
rb_assert!("range == (2..7)", range);
use magnus::rb_assert;
let range = magnus::Range::new(2, 7, true).unwrap();
rb_assert!("range == (2...7)", range);
sourcepub fn beg<T>(self) -> Result<T, Error>where
T: TryConvert,
pub fn beg<T>(self) -> Result<T, Error>where
T: TryConvert,
Return the value that defines the beginning of the range, converting it
to a T
.
Errors if the conversion fails.
§Examples
use magnus::eval;
let range: magnus::Range = eval("2..7").unwrap();
assert_eq!(range.beg::<i64>().unwrap(), 2);
sourcepub fn end<T>(self) -> Result<T, Error>where
T: TryConvert,
pub fn end<T>(self) -> Result<T, Error>where
T: TryConvert,
Return the value that defines the end of the range, converting it
to a T
.
Errors if the conversion fails.
§Examples
use magnus::eval;
let range: magnus::Range = eval("2..7").unwrap();
assert_eq!(range.end::<i64>().unwrap(), 7);
sourcepub fn excl(self) -> bool
pub fn excl(self) -> bool
Returns true
if the range excludes its end value, false
if the end
value is included.
§Examples
use magnus::eval;
let range: magnus::Range = eval("2..7").unwrap();
assert_eq!(range.excl(), false);
sourcepub fn beg_len(self, length: usize) -> Result<(usize, usize), Error>
pub fn beg_len(self, length: usize) -> Result<(usize, usize), Error>
Given a total length
, returns a beginning index and length of the
range within that total length.
Returns Err
if self
is a non-numerical range, or the range is out
of range for length
.
§Examples
use magnus::eval;
let range: magnus::Range = eval("2..").unwrap();
assert_eq!(range.beg_len(10).unwrap(), (2, 8));
use magnus::eval;
let range: magnus::Range = eval("..7").unwrap();
assert_eq!(range.beg_len(10).unwrap(), (0, 8));
use magnus::eval;
let range: magnus::Range = eval("-3..-1").unwrap();
assert_eq!(range.beg_len(10).unwrap(), (7, 3));
sourcepub fn to_range_with_len(self, length: usize) -> Result<StdRange<usize>, Error>
pub fn to_range_with_len(self, length: usize) -> Result<StdRange<usize>, Error>
Given a total length
, converts the Ruby Range
to a Rust
std::ops::Range
.
length
is required to account for Ruby conventions such as a range
from -2..-1
returning the end of a collection.
Returns Err
if self
is a non-numerical range, or the range is out
of range for length
.
§Examples
use magnus::eval;
// Ruby's .. range is inclusive
let range: magnus::Range = eval("2..7").unwrap();
// Rust's .. range in exclusive
assert_eq!(range.to_range_with_len(10).unwrap(), 2..8);
use magnus::eval;
let range: magnus::Range = eval("2..").unwrap();
assert_eq!(range.to_range_with_len(10).unwrap(), 2..10);
use magnus::eval;
let range: magnus::Range = eval("..7").unwrap();
assert_eq!(range.to_range_with_len(10).unwrap(), 0..8);
use magnus::eval;
let range: magnus::Range = eval("-3..-1").unwrap();
assert_eq!(range.to_range_with_len(10).unwrap(), 7..10);
Trait Implementations§
source§impl Object for Range
impl Object for Range
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 Range
impl ReprValue for Range
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