Continually experimenting with new ideas and techniques — Reconstructing, Developing, Modernising.
One of the things which has delighted me about Ruby ever since I discovered it is that its core and standard libraries cover so much useful ground, and I’m usually half way through implementing something before I stop, search, and discover the functionality was in the library all the time.
Last week at work I needed to generate all the possible values of a template like foo[1-3]bar[1,4,5], giving foo1bar1, foo1bar4, … foo3bar5.
It was easy enough to divide that up into creating a printf style template and expanding the ranges, so I had format = 'foo%dbar%d' and values = [[1, 2, 3], [1, 4, 5]]. I was about to write a routine to generate the product of an arbitrary number of arrays when I stopped and searched the web, and pretty quickly was reading about Array#product:
As Array#product is a method on an Array I had to pull out the first array from values to have something to call product on, and the way I chose to do that was something like this to return the array of expansions:
first, *rest = values
first.product(*rest).map do |v|
sprintf format, *v
As this was replacing a flawed implementation which generated all the combinations in memory and the typical use case has tens or hundreds of returned value this seemed to be an appropriate place to stop working.