I recently tried Opal builder for the first time to compile Ruby code to javascript, following this tutorial: https://github.com/opal/opal
However, even basic things like converting a number to an array or checking inequality with nil produce errors.
I hope this is just a newbie problem and there is a very simple answer. However, I cannot seem to find these particular problems or more comprehensive tutorial examples for the current Opal version (some details of the Builder e.g. seem to have changed over time).
I am using Ruby 2.4.0 and Opal 0.10.5. I use Firefox 52 to run the html file which embeds the generated js code.
The following snippet ...
# encoding: UTF-8
require "opal"
puts 2.to_a
... yields the following error in the Firefox console
to_a: undefined method 'to_a' for 2 (unknown)
The rakefile is
require 'opal'
require 'opal-jquery'
task :testing do
builder = Opal::Builder.new
builder.use_gem('opal-jquery')
File.open("testing.js", "w+") do |out|
out << builder.build("Testing.rb").to_s
end
end
And finally, the html file to wrap the js code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<link rel="stylesheet" href="styles.css"/>
</head>
<body>
<script src="testing.js"></script>
</body>
</html>
Any help is of course greatly appreciated.