Skip to content

Commit

Permalink
remove old-api functions from examples
Browse files Browse the repository at this point in the history
  • Loading branch information
matsadler committed Jul 27, 2024
1 parent 3a7f42c commit 26574af
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
20 changes: 12 additions & 8 deletions examples/complete_object/ext/temperature/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::{
};

use magnus::{
class, define_class, exception, method, module,
method,
prelude::*,
scan_args::{get_kwargs, scan_args},
typed_data, Error, Value,
typed_data, Error, Ruby, Value,
};

#[magnus::wrap(class = "Temperature", free_immediately, size)]
Expand Down Expand Up @@ -36,7 +36,11 @@ fn c_to_f(c: f64) -> f64 {
}

impl Temperature {
fn initialize(rb_self: typed_data::Obj<Self>, args: &[Value]) -> Result<(), Error> {
fn initialize(
ruby: &Ruby,
rb_self: typed_data::Obj<Self>,
args: &[Value],
) -> Result<(), Error> {
let args = scan_args::<(), (), (), (), _, ()>(args)?;
let kwargs = get_kwargs::<_, (), (Option<f64>, Option<f64>, Option<f64>), ()>(
args.keywords,
Expand All @@ -49,13 +53,13 @@ impl Temperature {
(None, None, Some(f)) => ((f_to_c(f) + C_OFFSET) * FACTOR) as u64,
(None, None, None) => {
return Err(Error::new(
exception::arg_error(),
ruby.exception_arg_error(),
"missing keyword: :kelvin, :celsius, or :fahrenheit",
))
}
_ => {
return Err(Error::new(
exception::arg_error(),
ruby.exception_arg_error(),
"unexpected keyword, supply one of: :kelvin, :celsius, or :fahrenheit",
))
}
Expand Down Expand Up @@ -83,8 +87,8 @@ impl fmt::Display for Temperature {
}

#[magnus::init]
fn init() -> Result<(), Error> {
let class = define_class("Temperature", class::object())?;
fn init(ruby: &Ruby) -> Result<(), Error> {
let class = ruby.define_class("Temperature", ruby.class_object())?;

// Define alloc func based on the Default impl, plus an initialize method,
// rather than overwriting `new`, to allow class to be subclassed from Ruby
Expand Down Expand Up @@ -113,7 +117,7 @@ fn init() -> Result<(), Error> {
// <=> sort operator based on Rust PartialOrd impl
class.define_method("<=>", method!(<Temperature as typed_data::Cmp>::cmp, 1))?;
// defines <, <=, >, >=, and == based on <=>
class.include_module(module::comparable())?;
class.include_module(ruby.module_comparable())?;

// debug/console output based on Rust Debug impl
class.define_method(
Expand Down
8 changes: 4 additions & 4 deletions examples/custom_exception_ruby/ext/ahriman/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use magnus::{
define_module, exception::ExceptionClass, function, gc::register_mark_object, prelude::*,
value::Lazy, Error, RModule, Ruby,
exception::ExceptionClass, function, gc::register_mark_object, prelude::*, value::Lazy, Error,
RModule, Ruby,
};

static RUBRIC_ERROR: Lazy<ExceptionClass> = Lazy::new(|ruby| {
Expand All @@ -25,8 +25,8 @@ fn cast_rubric(ruby: &Ruby) -> Result<(), Error> {
}

#[magnus::init]
fn init() -> Result<(), Error> {
let module = define_module("Ahriman")?;
fn init(ruby: &Ruby) -> Result<(), Error> {
let module = ruby.define_module("Ahriman")?;
module.define_singleton_method("cast_rubric", function!(cast_rubric, 0))?;
Ok(())
}
8 changes: 3 additions & 5 deletions examples/rust_blank/ext/rust_blank/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use magnus::{
class,
define_class,
encoding::{CType, RbEncoding},
method,
prelude::*,
Error, RString,
Error, RString, Ruby,
};

fn is_blank(rb_self: RString) -> Result<bool, Error> {
Expand Down Expand Up @@ -38,8 +36,8 @@ fn is_blank(rb_self: RString) -> Result<bool, Error> {
}

#[magnus::init]
fn init() -> Result<(), Error> {
let class = define_class("String", class::object())?;
fn init(ruby: &Ruby) -> Result<(), Error> {
let class = ruby.define_class("String", ruby.class_object())?;
class.define_method("blank?", method!(is_blank, 0))?;
Ok(())
}

0 comments on commit 26574af

Please sign in to comment.