This is a bit odd perhaps, but wondering if it is possible to achieve the following in a single line of Ruby code.
bar = 15
foo = 5
10.times { foo = foo + 1 }
puts foo == bar
i.e. I want to perform an action on a predefined variable a number of times, then compare it to another variable, returning a boolean based on whether they are equal, but in a single line of code?
Maybe
Enumerable#reducewill help.The number of iteration is passed as the object whose
reduceis being called. The initial value is passed asreduce‘s argument. The action is performed in the block and the result of each execution is carried through iterations as the first argument of the block.The only thing missing are assignments to variables. Let’s do it.
So yes, it is possible, however I’d recommend a verbose, multiline and readable solution.