Using a class which "includes" Enumerable

This is more of a Ruby question than an AWS question. It is about use of
“includes” of the Enumerable mix-in.

I am using the Amazon AWS Ruby (v1) SDK. I want to treat an S3
InstanceCollection as an Enumerable so I can use the ‘length’ and ‘at’
methods on it. According to the docs [1], InstanceCollection inherits
from Collection, which “includes” Enumerable. I’m not sure what that
means exactly, but I get a NoMethodError if I try to use length directly
on an InstanceCollection (see example below). Am I doing something
wrong, or does this sound like a bug in the API (or API docs)?

For the moment I am using the ‘each’ operation to iteratively create a
new Enumerable from the objects in the Collection, but that seems
inefficient and kind of silly.

Is there a way to more directly treat an InstanceCollection as an
Enumerable?

Here’s a sample of my problem:

irb(main):019:0> a=s3.buckets[bucketName].objects
=> AWS::S3::ObjectCollection
irb(main):020:0> a.length
NoMethodError: undefined method length' for <AWS::S3::ObjectCollection>:AWS::S3::ObjectCollection from (irb):20 from /usr/bin/irb:12:in
irb(main):021:0>

[1] API docs are at
http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/InstanceCollection.html

Bob Doolittle wrote in post #1178891:

I am using the Amazon AWS Ruby (v1) SDK. I want to treat an S3
InstanceCollection as an Enumerable so I can use the ‘length’ and ‘at’
methods on it.
[…]
irb(main):019:0> a=s3.buckets[bucketName].objects
=> AWS::S3::ObjectCollection
irb(main):020:0> a.length
NoMethodError: undefined method length' for <AWS::S3::ObjectCollection>:AWS::S3::ObjectCollection from (irb):20 from /usr/bin/irb:12:in
irb(main):021:0>

Well, first - though this doesn’t seem to be important here - it seems
that your object a is of type ObjectCollection, not InstanceCollection.

Indeed, ObjectCollection includes (according to the documentation)
Enumerable, but if you look at

http://ruby-doc.org/core-2.2.3/Enumerable.html

you will see that Enumerable offers neither “length” nor “at”.

It is surprising, that someone offers a collection class, without
providing a way to know the size of the collection, but Enumerable is
not to blame here. After all, what would be the meaning of the “length”
of an enumerable? An enumerable does not need to contained in a
“collection” in the classical sense of a data container, but could also
represent a (possibly infinite) set of values, such as a lazy list.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs