5 ms·
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)
by scottschulthess 15y ago
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)
- akmiller 15y agoYeah, 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.
- masklinn 15y agoI 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)
- akmiller 15y agoThat 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?
- masklinn 15y ago> 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?
- akmiller 15y agoI 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.
- burke 15y agoStrictly 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.
- deleted 15y ago[deleted]
- netghost 15y agoHis 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.
- grandalf 15y agoWhat do you mean?