Skip to content

Enumerators on Ruby 1.8/1.9 #8

Closed
bendiken opened this Issue Apr 18, 2010 · 1 comment

1 participant

@bendiken
Ruby RDF member

Prompted by a recent contribution to fix Ruby 1.9 enumerator compatibility (to be included in RDF.rb 0.1.8), I'm investigating what it will take to ensure that our use of enumerators is safe and compatible with all Ruby baseline versions that we wish to support (that is, 1.8.2+ and 1.9.x).

@bendiken
Ruby RDF member

The Ruby 1.8.x ChangeLog reveals that the enumerator standard library, which provides the Enumerable::Enumerator class, was added to Ruby back in 2003 and first debuted in the MRI 1.8.1 release.

Hence it is always safe to do an unconditional require 'enumerator' without breaking compatibility with any 1.8.2+ release (our current minimum required Ruby version). Therefore in RDF.rb 0.1.8 I have factored out the previously-used conditional inline require statements in favor of a single unconditional one in lib/rdf.rb that is executed when RDF.rb is first loaded up.

Now, Enumerable::Enumerator was renamed to Enumerator in the 1.9.0 release, without leaving a compatibility alias at the old name. So, to avoid having to use an incidental bit of conditional logic at every point where Enumerator objects are instantiated, I've introduced a convenient RDF::Enumerator constant in RDF.rb 0.1.8:

require 'enumerator'  # @since Ruby 1.8.1

module RDF
  # For compatibility with both Ruby 1.8.x and Ruby 1.9.x:
  Enumerator = defined?(::Enumerator) ? ::Enumerator : ::Enumerable::Enumerator
end

This means that any code contained within the RDF namespace (including RDF.rb plugins that keep to the recommended naming conventions) can now refer to enumerators using simply the class name Enumerator (as they would on Ruby 1.9+), and anything outside the RDF namespace (e.g. specs) can use the qualified RDF::Enumerator to considerably simplify backwards-compatible enumerator usage.

References:

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.