On Mon, Jun 25, 2012 at 4:59 PM, Intransition <[email protected]> wrote:
> On Monday, June 25, 2012 2:38:48 AM UTC-4, Robert Klemme wrote:
>>
>> What's the use case for this?
>
> In my case it's supplemental to another method I am working on.

And what's the use case for *that* method?

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

>       end
>     else
>       each_slice(2){ |k,v| h[k] = v }
>     end
>
>     h
>   end

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.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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