>
> 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.

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to