From: Robert Klemme Date: 2012-06-26T15:35:42+09:00 Subject: Re: Enumerable#unassociate On Mon, Jun 25, 2012 at 9:57 PM, Intransition wrote: >> On Monday, June 25, 2012 12:00:01 PM UTC-4, Robert Klemme wrote: >>> >>> And what's the use case for *that* method? > > >  To convert an Enumerable to a Hash. Sorry, I should have specified that. But not only that: you need a Hash as input to do transformations along the way. Wouldn't this be more reasonable if you just want to convert to a Hash? def to_hash {}.tap do |h| each_slice 2 do |k, v| k, v = yield k, v if block_given? h[k] = v end end end >>> >   def h(init={}) >>> >     h = init >>> >>> You never assign h nor init so you could completely get rid of h. > > > ??? What about `h[k] = v`, or am I misunderstanding? Yes. There is just one assignment to h. >>> I find it strange that you yield h[k] and not k.  The caller can never >>> know what k was, while if you pass k he can look up in init and obtain >>> the value you now yield. > > The idea behind that is to allow Symbol#to_proc to be useful. > >   a = [:a, 1, :b, 2, :a, 3] > >   i = Hash.new{ |h,k| h[k] = [] } >   a.h(i, &:+)  #=> {:a=>[1,3], :b=>[2]} irb(main):029:0> i = Hash.new{ |h,k| h[k] = [] } => {} irb(main):030:0> a.each_slice(2){|k,v| i[k] << v} => nil irb(main):031:0> i => {:a=>[1, 3], :b=>[2]} > I considered other interfaces to the block but this one seemed like it would > be the most useful b/c of this. > >>> It's totally mysterious what you are after here.  The fact that you do >>> not have a name yet might be indicative that this methods are probably >>> not such a good idea.  They do not look generally useful to me. > > >  No, that's me simply not giving enough initial context, which sometimes I > do so not to skew others consideration. But not very helpful in the case, > clearly. I agree with Bartosz, there is a lot of magic in there and the method does more than one thing which is always suspicious. If you want to do a transformation a la #map along the way I would leave that completely to a block passed and not restrict it to a Hash (well, a Proc might work as well - but still). Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/