pub struct Symbol(/* private fields */);
Expand description
A type wrapping either a StaticSymbol
or a Value pointer to a RSymbol
struct.
See the ReprValue
trait for additional methods available on this type.
See Ruby
for methods to create a Symbol
.
Implementations§
source§impl Symbol
impl Symbol
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(Symbol)
if val
is a Symbol
, None
otherwise.
§Examples
use magnus::{eval, Symbol};
assert!(Symbol::from_value(eval(":foo").unwrap()).is_some());
assert!(Symbol::from_value(eval(r#""bar".to_sym"#).unwrap()).is_some());
assert!(Symbol::from_value(eval(r#""baz""#).unwrap()).is_none());
sourcepub fn new<T: AsRef<str>>(name: T) -> Self
pub fn new<T: AsRef<str>>(name: T) -> Self
Create a new Symbol
from name
.
§Panics
Panics if called from a non-Ruby thread. See Ruby::to_symbol
for
the non-panicking version.
§Examples
use magnus::{rb_assert, Symbol};
let sym = Symbol::new("example");
rb_assert!(":example == sym", sym);
sourcepub fn is_static(self) -> bool
pub fn is_static(self) -> bool
Returns whether self
is static or not.
Static symbols won’t be garbage collected, so should be safe to store
on the heap. See StaticSymbol
.
§Examples
use magnus::{eval, Symbol};
assert!(eval::<Symbol>(":foo").unwrap().is_static());
assert!(!Symbol::new("bar").is_static());
assert!(!eval::<Symbol>(r#""baz".to_sym"#).unwrap().is_static());
sourcepub fn name(self) -> Result<Cow<'static, str>, Error>
pub fn name(self) -> Result<Cow<'static, str>, Error>
Return the symbol as a string. If the symbol is static this will be a
&str
, otherwise an owned String
.
May error if the name is not valid utf-8.
§Examples
use magnus::Symbol;
let sym = Symbol::new("example");
assert_eq!(sym.name().unwrap(), "example");
sourcepub fn as_static(self) -> Option<StaticSymbol>
pub fn as_static(self) -> Option<StaticSymbol>
If self
is static, returns self
as a StaticSymbol
, otherwise
returns None
.
§Examples
use magnus::{eval, Symbol};
assert!(eval::<Symbol>(":foo").unwrap().as_static().is_some());
assert!(Symbol::new("bar").as_static().is_none());
assert!(eval::<Symbol>(r#""baz".to_sym"#)
.unwrap()
.as_static()
.is_none());
sourcepub fn to_static(self) -> StaticSymbol
pub fn to_static(self) -> StaticSymbol
If self
is already static simply returns self
as a
StaticSymbol
. If self
is not static it will be made so and
returned as a StaticSymbol
.
Be aware that once static a symbol will never be garbage collected.
§Examples
use magnus::{rb_assert, Symbol};
let sym = Symbol::new("example");
let static_sym = sym.to_static();
rb_assert!("sym == static_sym", sym, static_sym);
Trait Implementations§
source§impl Borrow<Symbol> for StaticSymbol
impl Borrow<Symbol> for StaticSymbol
source§impl EncodingCapable for Symbol
impl EncodingCapable for Symbol
source§impl From<StaticSymbol> for Symbol
impl From<StaticSymbol> for Symbol
source§fn from(s: StaticSymbol) -> Self
fn from(s: StaticSymbol) -> Self
source§impl IntoSymbol for Symbol
impl IntoSymbol for Symbol
source§impl PartialEq<LazyId> for Symbol
impl PartialEq<LazyId> for Symbol
source§impl PartialEq<OpaqueId> for Symbol
impl PartialEq<OpaqueId> for Symbol
source§impl PartialEq<StaticSymbol> for Symbol
impl PartialEq<StaticSymbol> for Symbol
source§fn eq(&self, other: &StaticSymbol) -> bool
fn eq(&self, other: &StaticSymbol) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<Symbol> for Id
impl PartialEq<Symbol> for Id
source§impl PartialEq<Symbol> for LazyId
impl PartialEq<Symbol> for LazyId
source§impl PartialEq<Symbol> for OpaqueId
impl PartialEq<Symbol> for OpaqueId
source§impl PartialEq<Symbol> for StaticSymbol
impl PartialEq<Symbol> for StaticSymbol
source§impl PartialEq for Symbol
impl PartialEq for Symbol
source§impl ReprValue for Symbol
impl ReprValue for Symbol
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