From: Intransition Date: 2012-06-26T04:57:14+09:00 Subject: Re: Enumerable#unassociate ------=_Part_93_3469839.1340654231911 Content-Type: multipart/alternative; boundary="----=_Part_94_33538079.1340654231911" ------=_Part_94_33538079.1340654231911 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit > > 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. > I have no name for it yet, so call it #h. >> > >> > # Convert an Enumerable object to a Hash. >> > # >> > # [:a, 1, :b, 2].h >> > # #=> {:a=>1, :b=>2} >> > # >> > # [:a, 1, :b, 2, :a, 3].h{ |v0,v1| v0.to_i + v1 } >> > # #=> {:a=>4, :b=>2} >> > # >> > 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? > Btw, if you call the argument Hash "init" I would not necessarily >> expect it to be modified. Maybe it's better to do >> >> h = init.dup >> >> Or, to save one instantiation >> >> def h(init = nil) >> h = init ? init.dup : {} >> >> > if block_given? >> > each_slice(2) do |k,v| >> > h[k] = yield h[k], v >> >> 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]} 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. ------=_Part_94_33538079.1340654231911 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
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. 

> I have no name for it yet,= so call it #h. 
> 
>   # Convert an Enumerable = object to a Hash. 
>   # 
>   #   [:a,= 1, :b, 2].h 
>   #   #=3D> {:a=3D>1, :b=3D>2= } 
>   # 
>   #   [:a, 1, :b, 2, :a, 3= ].h{ |v0,v1| v0.to_i + v1 } 
>   #   #=3D> {:a=3D&g= t;4, :b=3D>2} 
>   # 
>   def h(init=3D{= }) 
>     h =3D init 

You never assign h n= or init so you could completely get rid of h.

??? What about `h[k] =3D v`, or am I misundersta= nding?
 
<= blockquote class=3D"gmail_quote" style=3D"margin-top: 0px; margin-right: 0p= x; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-l= eft-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;= ">Btw, if you call the argument Hash "init" I would not necessarily <= br>expect it to be modified.  Maybe it's better to do 

h = =3D init.dup 

Or, to save one instantiation 

def h(= init =3D nil) 
  h =3D init ? init.dup : {} 

> =     if block_given? 
>       each_slice= (2) do |k,v| 
>         h[k] =3D yield h[k],= v 

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. 
<= /blockquote>
 
The idea behind that is to allow Symbol#t= o_proc to be useful. 
 
  a =3D [:a, 1, :b, 2, :a, 3] 

  i =3D Hash.new{ |h,k| h[k] =3D [] }
 = ; a.h(i, &:+)  #=3D> {:a=3D>[1,3], :b=3D>= ;[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. &nbs= p;The fact that you do 
not have a name yet might be indicative tha= t this methods are probably 
not such a good idea.  They do no= t look generally useful to me.

 No, that's me simply not giving enough initial context, which someti= mes I do so not to skew others consideration. But not very helpful in the c= ase, clearly.
------=_Part_94_33538079.1340654231911-- ------=_Part_93_3469839.1340654231911--