I want to call the function f 5 times (for instance) and get a list of results. Right now I have this:
(1..5).to_a.map!{f}
Note: Right now f is a function that takes no input and returns true or false. So when this is done running, I get a list of 5 true/false values.
Is there a better way to do this using other built in functions (possibly reduce? I had that idea but cannot figure out how to use it…)
(Assuming no parameters.
mapis an alias tocollect; I prefer the namecollectwhen actually collecting as it seems more communicative, but YMMV.)I also prefer the longer
5.timesinstead of(1..5). This seems more communicative: I’m not really “iterating over a range”, I’m “doing something five times”.IMO the answer is slightly counter-intuitive in this case, since I’m not really running
collectfive times, butcollect.5.times { f }doesn’t work, so we play a bit of a mental game anyway.