Struct ruru::Class
[−]
[src]
pub struct Class { // some fields omitted }
Class
Also see def
, def_self
, define
and some more functions from Object
trait.
#[macro_use] extern crate ruru; use std::error::Error; use ruru::{Class, Fixnum, Object, VM}; methods!( Fixnum, itself, fn pow(exp: Fixnum) -> Fixnum { // `exp` is not a valid `Fixnum`, raise an exception if let Err(ref error) = exp { VM::raise(error.to_exception(), error.description()); } // We can safely unwrap here, because an exception was raised if `exp` is `Err` let exp = exp.unwrap().to_i64() as u32; Fixnum::new(itself.to_i64().pow(exp)) } ); fn main() { Class::from_existing("Fixnum").define(|itself| { itself.def("pow", pow); }); }
Ruby:
class Fixnum
def pow(exp)
raise TypeError unless exp.is_a?(Fixnum)
self ** exp
end
end
Methods
impl Class
[src]
fn new(name: &str, superclass: Option<&Self>) -> Self
Creates a new Class
.
superclass
can receive the following values:
None
to inherit fromObject
class (standard Ruby behavior when superclass is not given explicitly);Some(&Class)
to inherit from the given class
Examples
use ruru::{Class, VM}; let basic_record_class = Class::new("BasicRecord", None); assert_eq!(basic_record_class, Class::from_existing("BasicRecord")); assert_eq!(basic_record_class.superclass(), Some(Class::from_existing("Object"))); let record_class = Class::new("Record", Some(&basic_record_class)); assert_eq!(record_class, Class::from_existing("Record")); assert_eq!(record_class.superclass(), Some(Class::from_existing("BasicRecord")));
Ruby:
class BasicRecord
end
class Record < BasicRecord
end
BasicRecord.superclass == Object
Record.superclass == BasicRecord
fn from_existing(name: &str) -> Self
Retrieves an existing Class
object.
Examples
use ruru::{Class, VM}; let class = Class::new("Record", None); assert_eq!(class, Class::from_existing("Record"));
Ruby:
class Record
end
# get class
Record
# or
Object.const_get('Record')
fn new_instance(&self, arguments: Vec<AnyObject>) -> AnyObject
Creates a new instance of Class
Arguments must be passed as a vector of AnyObject
(see example).
Examples
use ruru::{Class, Fixnum, Object}; // Without arguments Class::from_existing("Hello").new_instance(vec![]); // With arguments passing arguments to constructor let arguments = vec![ Fixnum::new(1).to_any_object(), Fixnum::new(2).to_any_object() ]; Class::from_existing("Worker").new_instance(arguments);
Ruby:
Hello.new
Worker.new(1, 2)
fn superclass(&self) -> Option<Class>
Returns a superclass of the current class
Examples
use ruru::{Class, Object, VM}; assert_eq!( Class::from_existing("Array").superclass(), Some(Class::from_existing("Object")) ); assert_eq!(Class::from_existing("BasicObject").superclass(), None);
fn ancestors(&self) -> Vec<Class>
Returns a Vector of ancestors of current class
Examples
Getting all the ancestors
use ruru::{Class, VM}; let true_class_ancestors = Class::from_existing("TrueClass").ancestors(); let expected_ancestors = vec![ Class::from_existing("TrueClass"), Class::from_existing("Object"), Class::from_existing("Kernel"), Class::from_existing("BasicObject") ]; assert_eq!(true_class_ancestors, expected_ancestors);
Searching for an ancestor
use ruru::{Class, VM}; let basic_record_class = Class::new("BasicRecord", None); let record_class = Class::new("Record", Some(&basic_record_class)); let ancestors = record_class.ancestors(); assert!(ancestors.iter().any(|class| *class == basic_record_class));
fn get_nested_class(&self, name: &str) -> Self
Retrieves a Class
nested to current Class
.
Examples
use ruru::{Class, Object, VM}; Class::new("Outer", None).define(|itself| { itself.define_nested_class("Inner", None); }); Class::from_existing("Outer").get_nested_class("Inner");
Ruby:
class Outer
class Inner
end
end
Outer::Inner
# or
Outer.const_get('Inner')
fn define_nested_class(&mut self, name: &str, superclass: Option<&Class>) -> Self
Creates a new Class
nested into current class.
superclass
can receive the following values:
None
to inherit fromObject
class (standard Ruby behavior when superclass is not given explicitly);Some(&class)
to inherit from the given class
Examples
use ruru::{Class, Object, VM}; Class::new("Outer", None).define(|itself| { itself.define_nested_class("Inner", None); }); Class::from_existing("Outer").get_nested_class("Inner");
Ruby:
class Outer
class Inner
end
end
Outer::Inner
# or
Outer.const_get('Inner')
fn attr_reader(&mut self, name: &str)
Defines an attr_reader
for class
Examples
use ruru::{Class, Object, VM}; Class::new("Test", None).define(|itself| { itself.attr_reader("reader"); });
Ruby:
class Test
attr_reader :reader
end
fn attr_writer(&mut self, name: &str)
Defines an attr_writer
for class
Examples
use ruru::{Class, Object, VM}; Class::new("Test", None).define(|itself| { itself.attr_writer("writer"); });
Ruby:
class Test
attr_writer :writer
end
fn attr_accessor(&mut self, name: &str)
Defines an attr_accessor
for class
Examples
use ruru::{Class, Object, VM}; Class::new("Test", None).define(|itself| { itself.attr_accessor("accessor"); });
Ruby:
class Test
attr_accessor :accessor
end
Trait Implementations
impl Debug for Class
[src]
impl PartialEq for Class
[src]
fn eq(&self, __arg_0: &Class) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Class) -> bool
This method tests for !=
.
impl From<Value> for Class
[src]
impl Object for Class
[src]
fn value(&self) -> Value
Returns internal value
of current object. Read more
fn class(&self) -> Class
Returns a class of current object. Read more
fn singleton_class(&self) -> Class
Returns a singleton class of current object. Read more
fn define<F: Fn(&mut Self)>(&mut self, f: F) -> &Self
Wraps calls to the object. Read more
fn define_method<I: Object, O: Object>(&mut self, name: &str, callback: Callback<I, O>)
Defines an instance method for the given class or object. Read more
fn define_singleton_method<I: Object, O: Object>(&mut self, name: &str, callback: Callback<I, O>)
Defines a class method for given class or singleton method for object. Read more
fn def<I: Object, O: Object>(&mut self, name: &str, callback: Callback<I, O>)
An alias for define_method
(similar to Ruby syntax def some_method
).
fn def_self<I: Object, O: Object>(&mut self, name: &str, callback: Callback<I, O>)
An alias for define_singleton_method
(similar to Ruby def self.some_method
).
fn send(&self, method: &str, arguments: Vec<AnyObject>) -> AnyObject
Calls a given method on an object similarly to Ruby Object#send
method Read more
fn respond_to(&self, method: &str) -> bool
Checks whether the object responds to given method Read more
fn is_nil(&self) -> bool
Checks whether the object is nil
Read more
fn to_any_object(&self) -> AnyObject
Converts struct to AnyObject
Read more
fn instance_variable_get(&self, variable: &str) -> AnyObject
Gets an instance variable of object Read more
fn instance_variable_set<T: Object>(&mut self, variable: &str, value: T) -> AnyObject
Sets an instance variable for object Read more
unsafe fn to<T: Object>(&self) -> T
Unsafely casts current object to the specified Ruby type Read more
fn try_convert_to<T: VerifiedObject>(&self) -> Result<T>
Safely casts current object to the specified Ruby type Read more
fn ty(&self) -> ValueType
Determines the value type of the object Read more