Let's say I have an array of time lengths expressed in minutes:
minutes = [20, 30, 80]
I would like to sum the array contents and output the result in <hours>:<minutes>
format. For the example above I expect the result to be 02:10
.
Is there any standard Ruby method (i.e. included in core or std-lib) allowing to do this in an one line method chain? (i.e. without using a variable to store an intermediate result). I mean something like:
puts minutes.reduce(:+).foomethod { |e| sprintf('%02d:%02d', e / 60, e % 60) }
What should foomethod be? Object.tap is quite close to what I need, but unfortunately it returns self instead of the block result.
Try this one
puts sprintf('%02d:%02d', *minutes.reduce(:+).divmod(60))
proc { |e| sprintf('%02d:%02d', e / 60, e % 60) }.call(minutes.reduce(:+)) #=>"01:00"
or if you prefer lambdas
->(e) { sprintf('%02d:%02d', e / 60, e % 60) }.call(minutes.reduce(:+)) #=>"01:00"
PS: If you want to make these even shorter, you can also use []
and .()
for calling a lambda, i.e.
->(e) { sprintf('%02d:%02d', e / 60, e % 60) }.(minutes.reduce(:+)) #=>"01:00"
->(e) { sprintf('%02d:%02d', e / 60, e % 60) }[minutes.reduce(:+)] #=>"01:00"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With