-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ruby Bindings: Implement Enumerable for iterable collections #1781
base: main
Are you sure you want to change the base?
Conversation
0aacded
to
6ace8c9
Compare
6ace8c9
to
29910f3
Compare
I have implemented #each for collections that I could find, I was just looking for the implemented "begin" method, so not sure if I missed any collections that are also iterable. Other collections like Reldep or AdvisorySet do not have tests in Ruby so I have not extended them. I can take a stab at creating them if the tests are desired. |
29910f3
to
f40f9b3
Compare
f40f9b3
to
df80949
Compare
%extend ClassName { | ||
VALUE each() { | ||
// If no block is provided, returns Enumerator. | ||
RETURN_ENUMERATOR(swig::from($self), 0, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There probably should be RETURN_SIZED_ENUMERATOR
since we can get size beforehand, but the last argument to that macro is a function, that I am not quite sure how to supply or operate it in swig.
This should be a function that can be supplied for the RETURN_SIZED_ENUMERATOR
:
VALUE size_for_enumerator(VALUE collection, VALUE args, VALUE eobj) {
size_t size = $self->size();
return SIZET2NUM(size);
}
See:
/usr/include/ruby/internal/intern/enumerator.h
file in ruby-devel package
Inspired by Array from Ruby 2.7.0:
https://github.com/ruby/ruby/blob/v2_7_0/array.c#L2105
https://github.com/ruby/ruby/blob/v2_7_0/array.c#L2214
Implement including Enumerable and the #each method for iterable collections.
This allows for example to iterate over a package query:
Related issue: #1780