On Sun, 21 Apr 2013 07:56:50 +0200, Sbastien Durand <lists / ruby-forum.com> wrote:

> def options
>   options = {}
>   1.upto(31) { |i| options[i] = i }
>   options
> end
>
>
> def options
>   {}.tap { |options| 1.upto(31) { |i| options[i] = i } }
> end

Well, which one looks easier to read and understand? For me it's obviously the first one. Number of lines of code is not a metric of complexity.On Sun, 21 Apr 2013 08:43:15 +0200, tamouse mailing lists <tamouse.lists / gmail.com> wrote:

> def preset_options(n)
>   1.upto(n).reduce({}) {|o,i| o[i]=i; o}
> end

Uhh, and please don't do this, either. If you need multiple expressions inside reduce's block, you're doing it wrong.


If you really wanted to make it one expression, use a hash with default like Robert suggested, or something like `Hash[ (1..31).map{|n| [n, n] }]` - but frankly, your initial version looks the prettiest to me.

(On a completely unrelated note, because I felt like golfing: `Hash[ (1..31).map &Array.method(:new).to_proc.curry(2).call(2) ]`.)

-- Matma Rex