irb(main):038:0> (1..100).each_slice(5) {|x| p x}
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[11, 12, 13, 14, 15]
[16, 17, 18, 19, 20]
[21, 22, 23, 24, 25]
[26, 27, 28, 29, 30]
[31, 32, 33, 34, 35]
[36, 37, 38, 39, 40]
[41, 42, 43, 44, 45]
[46, 47, 48, 49, 50]
[51, 52, 53, 54, 55]
[56, 57, 58, 59, 60]
[61, 62, 63, 64, 65]
[66, 67, 68, 69, 70]
[71, 72, 73, 74, 75]
[76, 77, 78, 79, 80]
[81, 82, 83, 84, 85]
[86, 87, 88, 89, 90]
[91, 92, 93, 94, 95]
[96, 97, 98, 99, 100]
也许我在javascript中生锈了虽然已经为我的日常工作做了。我已经在js中攻击了好几天了。有什么建议吗?
提前谢谢!
答案 0 :(得分:1)
查看蛋白石! http://opalrb.org/try/
将代码复制并粘贴到那里......
如果您想知道如何开始使用原始html和javascript,请运行irb,然后输入以下内容:(执行gem install opal
后)
require 'opal'
File.open('opal.js', 'w') do |f|
f.write(Opal::Builder.build('opal'))
end
这将为您生成opal.js
为了获取ruby代码的javascript,你可以用irb:
puts Opal.compile("(1..100).each_slice(5) {|x| p x}")
您可以将ruby代码转换为javascript:
/* Generated by Opal 0.5.5 */
(function($opal) {
var $a, $b, TMP_1, self = $opal.top, $scope = $opal, nil = $opal.nil, $breaker = $opal.breaker, $slice = $opal.slice, $range = $opal.range;
$opal.add_stubs(['$each_slice', '$p']);
return ($a = ($b = ($range(1, 100, false))).$each_slice, $a._p = (TMP_1 = function(x){var self = TMP_1._s || this;if (x == null) x = nil;
return self.$p(x)}, TMP_1._s = self, TMP_1), $a).call($b, 5)
})(Opal);
现在在与opal.js相同的目录中创建一个新的html文件
<html>
<head>
<script src='opal.js'></script>
<script>
// copy the javascript above here
</script>
</head>
</html>
打开html文件并检查控制台,您应该看到:
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[11, 12, 13, 14, 15]
[16, 17, 18, 19, 20]
[21, 22, 23, 24, 25]
[26, 27, 28, 29, 30]
等等......