Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jackorp
Copy link

@jackorp jackorp commented Oct 17, 2024

Implement including Enumerable and the #each method for iterable collections.

This allows for example to iterate over a package query:

require 'libdnf5/base'

base = Base::Base.new
base.setup()

repo_sack = base.get_repo_sack()
repo_sack.create_repos_from_system_configuration()

rq = Repo::RepoQuery.new(base)
rq.filter_id('rawhide')

repo_sack.load_repos(Repo::Repo::Type_AVAILABLE)

query = Rpm::PackageQuery.new(base)
query.filter_provides("rubygem(httparty)", Common::QueryCmp_EQ)

query.each # Returns => #<Enumerator: ...>
query.each do |pkg|
  puts pkg.get_name
end
# Prints => rubygem-httparty
# And Returns => #<Rpm::PackageSet:0x00007f14d9ecd9a8 @__swigtype__="_p_libdnf5__rpm__PackageSet">

Related issue: #1780

@jackorp jackorp marked this pull request as draft October 17, 2024 17:47
@jackorp jackorp marked this pull request as ready for review October 18, 2024 10:43
@jackorp
Copy link
Author

jackorp commented Oct 18, 2024

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.

@jackorp jackorp changed the title Draft: Ruby Bindings: Implement Enumerable for iterable collections Ruby Bindings: Implement Enumerable for iterable collections Oct 18, 2024
%extend ClassName {
VALUE each() {
// If no block is provided, returns Enumerator.
RETURN_ENUMERATOR(swig::from($self), 0, 0);
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant