From: Andrew Wagner Date: 2010-12-01T08:00:14+09:00 Subject: Re: Object#each ? --0016e6d58a6f92602b04964d2656 Content-Type: text/plain; charset=UTF-8 On Tue, Nov 30, 2010 at 4:40 PM, Robert Klemme wrote: > On 11/30/2010 06:45 PM, Andrew Wagner wrote: > >> On Tue, Nov 30, 2010 at 12:44 PM, Andrew Wagner> >wrote: >> >> A weird idea just popped into my head: >>> >>> class Object >>> def each >>> yield self >>> end >>> end >>> >>> Lets you enumerate without having to worry about whether or not you have >>> a >>> collection. Thoughts? Hidden ramifications? >>> >> > First of all if you implement #each then you should also include Enumerable > because the expectation would be that you also have all the nice methods > like #select, #map etc. This makes all objects Enumerable which does not > seem a good idea. An interesting point. I think I agree that cluttering Object with all the Enumerable methods seems like a bad idea. I guess my thinking was that this would be useful just for the purposes of having #each available, without including Enumerable. This avoids the [*items] garbage. > One drawback of this approach is that you won't notice if you accidentally > pass something to a method which should be Enumerable (I mean a real > collection) - or at least the issue might take some time to be detected. This is an interesting argument, because it sounds an awful lot like the arguments I usually hear against duck-typing and dynamic typing in general. Don't those also allow issues which "might take some time to be detected" too? It seems like the answer is, as always, that's what specs/tests are for. One other thing that I thought of after my original email is that it might be preferable to change it to "yield self unless nil?", because it could be odd to have NilClass#each. Thanks for the thoughts! --0016e6d58a6f92602b04964d2656--