News for hackers. Stuff that matters.
Re:The State of Ruby ORM by scottschulthess on Tuesday November 29, 2011 @11:28AM (#3290930)
His example

User.where(:active => true).kind_of?(Enumerable)

What's the issue here? If you change it to

User.where(:active => true).all.kind_of?(Enumerable)

Re:The State of Ruby ORM by akmiller on Tuesday November 29, 2011 @11:37AM (#3290966)
Yeah, I'm not sure I understand that either. The whole point is that with the first example it is NOT an Enumerable yet. You are building, what is essentially, a query object and until you access something that query will not be executed at which point it will be an Enumerable as you point to with the "all" method.
Re:The State of Ruby ORM by masklinn on Tuesday November 29, 2011 @11:54AM (#3291041)
I guess TFAA would like the query under construction to be Enumerable, and Enumerating it to serialize it without needing the `all` indirection. Something similar to LINQ.

In fact, some of Enumerable's messages could even be used to further build the query (e.g. `Enumerable#drop(Integer)` simply bumps the query's offset)

Re:The State of Ruby ORM by akmiller on Tuesday November 29, 2011 @12:15PM (#3291133)
That makes some sense to me, but technically that object at that point is not an "Enumerable" so you are essentially faking it knowing that it would be once executed.

Is there some significant benefit to it returning true for Enumerable that I'm just missing?

Re:The State of Ruby ORM by masklinn on Tuesday November 29, 2011 @01:32PM (#3291509)
> That makes some sense to me, but technically that object at that point is not an "Enumerable"

Why not? An enumerable is anything that can be enumerated, the query object holds the potential of being enumerated, it's just an actual underlying query away but that's of no relevance to the interface. The only reason why it's not "technically Enumerable" at that point is... that it does not implement Enumerable's interface.

> so you are essentially faking it knowing that it would be once executed.

Why would you be faking anything? Is LINQ faking anything when it produces IEnumerable<T> after each "operation"?

> Is there some significant benefit to it returning true for Enumerable that I'm just missing?

Interface simplicity? Workflow simplicity? I see no reason for it not to? Get rid of the useless `all` method?

Re:The State of Ruby ORM by burke on Tuesday November 29, 2011 @02:46PM (#3291853)
Strictly speaking, you're not actually enumerating for a lot of the potential use-cases of enumerable methods.

For example, say #reject were implemented:

    User.where(admin: true).reject(office: current_user.office)
There are two ways to implement this: One by literally iterating over the result set and filtering it; and Two by adding a projection to the query builder.

In the second case, it is small-e enumerable, but not strictly Enumerable, since Enumerable very explicitly defines each method in terms of #each. That doesn't make sense for a query builder.

It could implement Enumerable and override every single method, but the point a few people are making here and in the post comments is that checking kind_of?(Enumerable) is non-idiomatic. Including modules for semantic meaning is appealing to programmers from certain backgrounds for completely understandable reasons, but simply checking for the presence of the method needed is more clear -- ie. respond_to?(:each) is the intended solution.

TL;DR: Modules aren't Interfaces.

Re:The State of Ruby ORM by akmiller on Tuesday November 29, 2011 @02:35PM (#3291799)
I don't think there is much difference between what ActiveRecord is doing and what LINQ does.

The main LINQ object is an IQueryable object that implements IEnumerable. However, IEnumerable is not the object that has all the enumerator methods on it. That is IEnumerator. The only method IEnumerable has is the GetEnumerator method which returns the IEnumerator object.

So, in actuality, the "all" method in ActiveRecord kind of acts like the IEnumerable interface in returning the actual enumerator object.

Re:The State of Ruby ORM by netghost on Tuesday November 29, 2011 @01:16PM (#3291413)
His example is really just silly. ActiveRecord is doing the right thing here, it lets you chain your methods until you do something that requires it to hit the database and actually fetch the data.
Re:The State of Ruby ORM by grandalf on Tuesday November 29, 2011 @02:18PM (#3291725)
What do you mean?