We can use Fixnum#divmod here
irb(main):004:0> 61.times.map {|x| a,b=x.divmod 60; 800+a*100+b}
=> [800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812,
813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826,
827, 828, 829, 830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840,
841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854,
855, 856, 857, 858, 859, 900]
For times I'd probably rather use Time though:
irb(main):012:0> start = Time.local 2012,9,13,8
=> 2012-09-13 08:00:00 +0200
irb(main):013:0> 61.times.map {|m| start + m * 60}
=> [2012-09-13 08:00:00 +0200, 2012-09-13 08:01:00 +0200, 2012-09-13
08:02:00 +0200, 2012-09-13 08:03:00 +0200, 2012-09-13 08:04:00 +0200,
2012-09-13 08:05:00 +0200, 2012-09-13 08:06:00 +0200, 2012-09-13
08:07:00 +0200, 2012-09-13 08:08:00 +0200, 2012-09-13 08:09:00 +0200,
2012-09-13 08:10:00 +0200, 2012-09-13 08:11:00 +0200, 2012-09-13
08:12:00 +0200, 2012-09-13 08:13:00 +0200, 2012-09-13 08:14:00 +0200,
2012-09-13 08:15:00 +0200, 2012-09-13 08:16:00 +0200, 2012-09-13
08:17:00 +0200, 2012-09-13 08:18:00 +0200, 2012-09-13 08:19:00 +0200,
2012-09-13 08:20:00 +0200, 2012-09-13 08:21:00 +0200, 2012-09-13
08:22:00 +0200, 2012-09-13 08:23:00 +0200, 2012-09-13 08:24:00 +0200,
2012-09-13 08:25:00 +0200, 2012-09-13 08:26:00 +0200, 2012-09-13
08:27:00 +0200, 2012-09-13 08:28:00 +0200, 2012-09-13 08:29:00 +0200,
2012-09-13 08:30:00 +0200, 2012-09-13 08:31:00 +0200, 2012-09-13
08:32:00 +0200, 2012-09-13 08:33:00 +0200, 2012-09-13 08:34:00 +0200,
2012-09-13 08:35:00 +0200, 2012-09-13 08:36:00 +0200, 2012-09-13
08:37:00 +0200, 2012-09-13 08:38:00 +0200, 2012-09-13 08:39:00 +0200,
2012-09-13 08:40:00 +0200, 2012-09-13 08:41:00 +0200, 2012-09-13
08:42:00 +0200, 2012-09-13 08:43:00 +0200, 2012-09-13 08:44:00 +0200,
2012-09-13 08:45:00 +0200, 2012-09-13 08:46:00 +0200, 2012-09-13
08:47:00 +0200, 2012-09-13 08:48:00 +0200, 2012-09-13 08:49:00 +0200,
2012-09-13 08:50:00 +0200, 2012-09-13 08:51:00 +0200, 2012-09-13
08:52:00 +0200, 2012-09-13 08:53:00 +0200, 2012-09-13 08:54:00 +0200,
2012-09-13 08:55:00 +0200, 2012-09-13 08:56:00 +0200, 2012-09-13
08:57:00 +0200, 2012-09-13 08:58:00 +0200, 2012-09-13 08:59:00 +0200,
2012-09-13 09:00:00 +0200]
Kind regards
robert
···
On Thu, Sep 13, 2012 at 2:42 PM, Sung Pae <sungpae@gmail.com> wrote:
On Thu, Sep 13, 2012 at 08:31:44PM +0900, Jermaine O. wrote:
(800..900).to_a (which represents: 08:00 - 09:00) will generate an
array something like this:
#=> [800,801,802,...... 899, 900]
Whereas I would like the output to be like this:
#=> [800,801,802,...... 859, 900]
Basically it should bump to the next major number (hour) after 59
instead of going all the way up to 99.
Do you just want to throw away values where the mod(100) values are
greater than 59?
(800..900).reject { |n| (n % 100) > 59 }
# => [800, 801, 802, 803, …, 900]
Or do you want to map base-10 numbers to a kind of untyped base-60
format?
(800..900).map { |n| (n / 60)*100 + (n % 60) }
# => [1320, 1321, 1322, 1323, 1324, 1325, 1326, 1327, 1328, 1329,
1330, 1331, 1332, 1333, 1334, 1335, 1336, 1337, 1338, 1339, 1340,
1341, 1342, 1343, 1344, 1345, 1346, 1347, 1348, 1349, 1350, 1351,
1352, 1353, 1354, 1355, 1356, 1357, 1358, 1359, 1400, 1401, 1402,
1403, 1404, 1405, 1406, 1407, 1408, 1409, 1410, 1411, 1412, 1413,
1414, 1415, 1416, 1417, 1418, 1419, 1420, 1421, 1422, 1423, 1424,
1425, 1426, 1427, 1428, 1429, 1430, 1431, 1432, 1433, 1434, 1435,
1436, 1437, 1438, 1439, 1440, 1441, 1442, 1443, 1444, 1445, 1446,
1447, 1448, 1449, 1450, 1451, 1452, 1453, 1454, 1455, 1456, 1457,
1458, 1459, 1500]
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/