From: tamouse mailing lists Date: 2013-04-21T15:43:15+09:00 Subject: Re: Tap method : good or bad practice ? On Sun, Apr 21, 2013 at 12:56 AM, Sébastien Durand wrote: > Hi all ! > > I try to code a function that generates and returns options. Is it > better to do this : > > > def options > options = {} > 1.upto(31) { |i| options[i] = i } > options > end > > > Or this : > > > def options > {}.tap { |options| 1.upto(31) { |i| options[i] = i } } > end > > > What is the Ruby way of doing this ? Is it common to use this > trap method ? Thank you very much for your help (I'm new to Ruby) ! There is more than one way to do things. def preset_options(n) 1.upto(n).reduce({}) {|o,i| o[i]=i; o} end The above doesn't need to be wrapped in a method, though if it is an oft-repeated motif, it should be.