Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating this array programmatically?

I'm currently populating a simple array like so:

queues = %w(rate_limit_000 rate_limit_001 rate_limit_002 rate_limit_003 rate_limit_004 rate_limit_005 rate_limit_006 rate_limit_007 rate_limit_008 rate_limit_009 rate_limit_010 rate_limit_011 rate_limit_012)

That's ripe for refactoring. So what's the easiest way to build that array without manually adding items?

The only difference between the item names are those last 3 digits, which should always be 3 digits, but I do need to set a limit on how high it goes.

like image 602
Shpigford Avatar asked Dec 28 '25 10:12

Shpigford


1 Answers

What about using String#%?

(0..12).map { |i| "rate_limit_%03d" % i }
# => ["rate_limit_000", "rate_limit_001", "rate_limit_002", ...
like image 104
toro2k Avatar answered Dec 31 '25 01:12

toro2k