2

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.

  • You say "There are two Ruby snippets I am using" but can you please pick one of the two? It's a little awkward to try to solve two potentially different problems. Otherwise a well-written question. – Jared Beck Apr 18 '18 at 00:30
  • Thanks for the hint, I removed the second issue from the question.My "hope" actually was that there is a common underlying issue like a missing library or the like that I just did not find. – user3385488 Apr 19 '18 at 18:50
  • 1
    What do you expect `puts 2.to_a` to print? – Jordan Running Apr 19 '18 at 18:58

0 Answers0