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) ].)
Robert the Problem is the default proc, it is maybe not what he wants
because it pretend from marshaling
Why is that a problem? Do you have knowledge about the situation of
Sbastien which other writers in this thread do not have? If not, why
are
you claiming there is problem?
Regardless of the refactors people are suggesting, going to the original
question about tap I know people who like it and people who dislike it.
From my perspective I don’t think there’s consensus as to which form is
preferred.
Most of the times I use tap, because it conveys the intention to the
reader
that you are going to return that value. It is kind of declarative for
me.
But in a method like yours where there’s three lines I would not
normally
use it, since I believe people load those 3 lines in one shot in their
head
and the form without tap is probably more readable.
On Sun, Apr 21, 2013 at 12:56 AM, Sbastien D. [email protected]wrote:
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) !
I wouldn’t do that, unless I had a need to inline it (e.g. writing a
one-liner on the the command line), but I also wouldn’t care enough to
change it if I saw it in my codebase. It is totally irrelevant next to
concerns about object boundaries and mutability, so it seems to me that
any
disagreement is just bikeshedding.
On Sun, Apr 21, 2013 at 1:20 PM, Love U Ruby [email protected]
wrote:
Can on the same context anyone explain the difference between tap and inject ?
On Sun, Apr 21, 2013 at 12:56 AM, Sébastien Durand [email protected]
wrote:
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) !
I wouldn’t do that, unless I had a need to inline it (e.g. writing a
one-liner on the the command line), but I also wouldn’t care enough to
change it if I saw it in my codebase. It is totally irrelevant next to
concerns about object boundaries and mutability, so it seems to me that
any
disagreement is just bikeshedding.
The thing I don’t like about using .tap in the way sown is that it feels
like a side effect, but admittedly that comes from my initial belief
about
the intention of .tap. As with most things, we find new uses for things
that weren’t originally thought of, and I find that good for the most
part.
The thing I don’t like about using .tap in the way sown is that it feels
like a side effect, but admittedly that comes from my initial belief
about
the intention of .tap. As with most things, we find new uses for things
that weren’t originally thought of, and I find that good for the most
part.
I often find myself using idioms like
Klass.new.tap do
…
end
with Klass = Hash, Array, … , and dreaming about a shortcut to
‘new.tap’ like ‘such_that’, ‘with’, …
That doesn’t make any sense at all: there is no point in using #tap if
you
do not assign the result and not use the block parameter. (Note: #tap
does
not change self.) Or do you mean
Klass.new.instance_eval do
…
end
with Klass = Hash, Array, … , and dreaming about a shortcut to
‘new.tap’ like ‘such_that’, ‘with’, …
What is it that you want to achieve? Can you please come up with a more
realistic example?
Why this kind of loop from 1 to 31 ? These magic numbers are the days in
a month and I use them in a DaySelect class.
I decided to go with the most explicit implementation :
def options
options = {}
‘1’.upto(‘31’) do |day| options[day] = ‘%02d’ % day end
options
end
“tap”, “each_with_object” and so on are maybe overkill ? But
being new to
Ruby, this makes me realize that there is more than one way to do it and
Ruby fits everybody’s style.
(By the way, sorry for my poor english, I’m a french-speaking guy.)