matsadler / magnus

Ruby bindings for Rust. Write Ruby extension gems in Rust, or call Ruby from Rust.
https://docs.rs/magnus/latest/magnus/
MIT License
682 stars 35 forks source link

Confused about how to pass a Ruby array to a Rust function #117

Closed kingsleyh closed 3 months ago

kingsleyh commented 3 months ago

How do I pass a ruby array to a rust function

Utils.run('string1', 'string2', ['apple','pear'])
pub fn run(ruby: &Ruby, param1: String, param2: String, param3: RArray) -> magnus::error::Result<()> {
    let list = param3.to_vec().iter().map(|v| v.to_string()).collect();
}

#[magnus::init]
fn init() -> Result<(), magnus::Error> {
    let module = define_module("Utils")?;
    module.define_singleton_method("run", function!(run, 3))?;
    Ok(())
}

This doesn't work - but I don't get any error - it' just fails with unknown error when trying to write an Rspec test

Is what I'm trying to do supported in Magnus? or I need to pass named arguments into the ruby and handle them as an object in rust?

e.g. Utils.run(string1:, string2:, list: ) and in rust pub fn run(ruby:&Ruby, ???? some way to handle the named parameters)

kingsleyh commented 3 months ago

nevermind - there was something wrong with my environment and nothing was working - but after deleting and re-cloning the repo things started working as expected - weird!