each_slice like in javascript from ruby using opal

<>

This question already has an answer here:

Answers


Check out Opal! http://opalrb.org/try/

Copy and paste your code there...

If you're wondering how to get started with raw html and javascript, run irb, and type the following: (after doing gem install opal)

require 'opal'
File.open('opal.js', 'w') do |f|
  f.write(Opal::Builder.build('opal'))
end

This will generate opal.js for you

In order to get the javascript for your ruby code, you can do in irb:

puts Opal.compile("(1..100).each_slice(5) {|x| p x}")

You get the conversion of your ruby code to 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);

Now create a new html file in the same directory as opal.js

<html>
<head>
<script src='opal.js'></script>
<script>
// copy the javascript above here
</script>
</head>
</html>

Open the html file and check the console, you should see:

[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]

etc...


Need Your Help

Add a cell formula in Excel via vba

excel vba excel-vba formula

I’m not an Excel or VBA expert but I want to insert this current excel formula into cell’s using VBA.

How do I change the URL for the wordpress author archive page?

wordpress .htaccess mod-rewrite

Instead of www.example.com/author/xyz, I want to use www.example.com/artist/xyz.