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 RFE: Implement iteration through Ruby's Enumerable module. #1780

Open
jackorp opened this issue Oct 17, 2024 · 0 comments
Open

Comments

@jackorp
Copy link

jackorp commented Oct 17, 2024

Iterating over collections (e.g. ReldepList, PackageQuery, ...) in Ruby currently requires a quite unwieldy while loop.
To get elements out of PackageQuery for example:

item = collection.begin
c_end = collection.end

while item != c_end
  puts item.value.get_name
  item = item.next
end

In idiomatic ruby, the methods from Enumerable module are preferred. The mentioned loop should look like:

collection.each do |item|
  puts item.get_name
end

For any iterable collection, there are 2 requirements are needed to make use of the enumerable module:

  1. including Enumerable module in the class to get the methods
  2. implement instance method each that iterates over each element, yielding it in a block.

After just these 2 steps we also acquire other methods such as #map, #find, #select, #inject, etc... from Enumerable.

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

No branches or pull requests

1 participant