You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a scenario where I need to pass an array to a ruby proc. While using magnus proc I noticed that it treats the argument as a RArrayArgList when passing it to proc - this will mean that it gets passed as head, *rest to the ruby proc which is not idea. At the moment I just have a hash wraping it and will do a type check on the arg to the proc. Is there a way to allow arrays as arguments to procs (I tried wrapping in another array but no luck) or is there a better way to wrap the argument to the proc?
The text was updated successfully, but these errors were encountered:
I'm unsure of your exact problem as you don't provide an example, but here's a dump of what I know related to your question.
The underlying Ruby API, rb_proc_call, takes its arguments list as a Ruby Array. To make things more convenient to use, rather than have you create a Ruby Array of your arguments yourself, Magnus' Proc::call takes it's arguments list as any type implementing RArrayArgList.
The types that implement RArrayArgList are, Ruby Arrays (so if you happen to already have a Ruby Array of your arguments list, then just pass that), and types that implement ArgList (ArgList is also used for functions that take their arguments list as a C array). The types that implement ArgList are tuples of any type that can be converted to a Ruby type, or a (Rust) array of Ruby types, or a slice of Ruby types.
let ary = ruby.ary_from_iter([1,2,3]);
some_proc.call((ary,))// trailing comma denotes a tuple of 1 value// orlet ary = ruby.ary_from_iter([1,2,3]);
some_proc.call([ary])// orlet ary = ruby.ary_from_iter([1,2,3]);let args = ruby.ary_new_from_values(&[ary]);
some_proc.call(args)
I think the problem you might be running into is that given a proc with arguments like |head, *rest| when calling that proc with a single array argument Ruby will automatically expand the array as if it was an arguments list, e.g. in pure Ruby:
x=proc{|head, *rest| puts"head: #{head.inspect}, rest: #{rest.inspect}"}x.call(1,2,3)# prints "head: 1, rest: [2, 3]"x.call([1,2,3])# prints "head: 1, rest: [2, 3]"
This is a feature of Ruby that Magnus has no control over.
I have a scenario where I need to pass an array to a ruby proc. While using magnus proc I noticed that it treats the argument as a
RArrayArgList
when passing it to proc - this will mean that it gets passed ashead, *rest
to the ruby proc which is not idea. At the moment I just have a hash wraping it and will do a type check on the arg to the proc. Is there a way to allow arrays as arguments to procs (I tried wrapping in another array but no luck) or is there a better way to wrap the argument to the proc?The text was updated successfully, but these errors were encountered: