These are chat archives for opal/opal

9th
Jan 2016
Jacob Evelyn
@JacobEvelyn
Jan 09 2016 02:52 UTC
This feels like a very dumb question, but I've got a Ruby that runs Opal::Builder.build and outputs my Ruby program in JavaScript. My program performs some File I/O and I'd like to use the nodejs File implementation in the stdlib folder... but I'm not sure how to get Opal to load that. I don't see use of stdlib documented anywhere but I know I must be missing something obvious.
Vais Salikhov
@vais
Jan 09 2016 04:39 UTC
@JacobEvelyn require 'nodejs' should do it I think...
Jacob Evelyn
@JacobEvelyn
Jan 09 2016 05:47 UTC
@vais thanks for the help. That seems to have worked but only when I manually add in the glob npm module. Is there a way to get that automatically included since it's in the package.json?
Bernhard Weichel
@bwl21
Jan 09 2016 09:33 UTC
where can I find information about Native vs. JS to deal with Javascript objects. Do the two approaches compete or complement each other?
Vais Salikhov
@vais
Jan 09 2016 11:31 UTC
@JacobEvelyn that I'm not sure about
@bwl21 pretty much here: http://opalrb.org/docs/guides/v0.9.0/compiled_ruby.html
(scroll down to the "JavaScript from Ruby" section, it covers the Native module and the new recv.JS.method syntax)
Elia Schito
@elia
Jan 09 2016 12:03 UTC
@JacobEvelyn there's also require 'opal/platform' available from v0.9 onwards that is a bit more minimal and doesn't need glob (because it doesn't require file/dir methods)
As for the node_modules dir I was thinking about inlining them (given the dep tree is reasonably manageable)
as an alternative tho you can run opal code on node with the CLI: opal -ropal/platform -Rnodejs -e 'puts 5'runs the code on nodejs (see -h for more details/options)
Guillaume Grossetie
@Mogztter
Jan 09 2016 14:26 UTC
Hi, is there a way to tell Opal's command line to put the generated JavaScript code in a module ?
I'm using the following command: opal --compile -I lib -I asciidoctor -g asciidoctor -r asciidoctor/converter/docbook5 --dynamic-require ignore --no-opal --no-exit > build/asciidoctor-docbook5.js < /dev/null
and I want something like: Opal.modules["asciidoctor/converter/docbook5"] = function(Opal) { instead I get (function(Opal) {
I'm using Opal v0.9.0.beta2
Elia Schito
@elia
Jan 09 2016 14:31 UTC
@Mogztter that's not currently supported but I'm about to release 0.9.1 and if we find a good CLI Api it could make in it
Guillaume Grossetie
@Mogztter
Jan 09 2016 14:32 UTC
Ok, do you want me to open an issue to track down the progress ?
And do you know if I can avoid the generation of an empty block when the source code is empty from the command line ?
When source code is empty, the following block is still generated:
(function(Opal) {
  Opal.dynamic_require_severity = "ignore";
  var OPAL_CONFIG = { method_missing: true, arity_check: false, freezing: true, tainting: true };
  var self = Opal.top, $scope = Opal, nil = Opal.nil, $breaker = Opal.breaker, $slice = Opal.slice;

  return nil
})(Opal);
Elia Schito
@elia
Jan 09 2016 16:01 UTC
Yes please, open the issue, maybe a --module CLI option could serve to compile without a main file
otherwise the main file could be optional if a compilation is requested
@Mogztter ^
Torstein Saltvedt
@saltvedt
Jan 09 2016 17:03 UTC
hi
I'm following this tutorial: http://www.sitepoint.com/opal-ruby-browser-game-life/
but I'm stuck due to:
NameError: uninitialized constant Opal::Environment /Users/torstein/code/game-of-life/Rakefile:7:in block in <top (required)>'
if I do a Opal.constants.inspect just above that line, I get [:Sexp, :REGEXP_START, :REGEXP_END, :Keywords, :Lexer, :Parser, :ParserScope, :Fragment, :Nodes, :VERSION, :Compiler, :HikePathFinder, :PathReader, :ERB, :BuilderProcessors, :UseGem, :Builder, :Config, :TiltTemplate, :Sprockets, :SourceMapServer, :Processor, :SourceMap, :Server, :JQuery] so it looks like Opal itself is correctly loaded.
any ideas?
Elia Schito
@elia
Jan 09 2016 17:04 UTC
You should be able to replace it with:
environment = Sprockets::Environment.new
Opal.paths.each do |path|
  environment.append_path path
end
Opal::Environment was deprecated and removed (or not autorequired anymore), there's has been some talk to add it back but it didn't happen so far
Torstein Saltvedt
@saltvedt
Jan 09 2016 17:11 UTC
thanks! got it working by adding a Opal.append_path 'app' to your suggestion :)
Elia Schito
@elia
Jan 09 2016 17:16 UTC
:+1:
Torstein Saltvedt
@saltvedt
Jan 09 2016 17:24 UTC
puts should print to the console, right?
code gets compiled and everything is loaded correctly, but nothing is printed in the javascript console in the browser
Elia Schito
@elia
Jan 09 2016 17:25 UTC
Yes, in the browser at least
@saltvedt try with require 'opal/platform'
Torstein Saltvedt
@saltvedt
Jan 09 2016 17:28 UTC
instead of just require 'opal'? just gives me Uncaught ReferenceError: Opal is not defined
Elia Schito
@elia
Jan 09 2016 17:30 UTC
no, require opal is still necessary
opal/platform will load stdout adapters based on the platform you're running your code on (eg. browser, phantomjs, nodejs, etc)
Torstein Saltvedt
@saltvedt
Jan 09 2016 17:33 UTC
hmm...
/* Generated by Opal 0.9.0 */
Opal.modules["game-of-life"] = function(Opal) {
  Opal.dynamic_require_severity = "error";
  var OPAL_CONFIG = { method_missing: true, arity_check: false, freezing: true, tainting: true };
  var self = Opal.top, $scope = Opal, nil = Opal.nil, $breaker = Opal.breaker, $slice = Opal.slice;

  Opal.add_stubs(['$require', '$puts']);
  self.$require("opal");
  self.$require("opal/platform");
  return self.$puts("hello, world");
};
console.log('hello?');
only "hello?" appears in the console
(there is a ton of stuff above)
Elia Schito
@elia
Jan 09 2016 17:43 UTC
didn't check out the tutorial, but the way opal works WRT files is that it saves them to Opal.modules as in the snippet above
and needs, after all modules are ready, a final load for the main module
likely: Opal.load("game-of-life")
@saltvedt ^
Torstein Saltvedt
@saltvedt
Jan 09 2016 17:50 UTC
yes, now it works
thank you again :)
guess this tutorial is a bit dated
Elia Schito
@elia
Jan 09 2016 17:53 UTC
@saltvedt my pleasure! I think Benjamin will be happy to update it if you send him the adjustments you had to make

@/all https://twitter.com/rubygems/status/685870824004448256

What's new:

  • Backport rack2 compatibility (#1260)

  • Fixed issue with JS nil return paths being treated as true (#1274)

  • Fix using more than two rescue in sequence (#1269)

Torstein Saltvedt
@saltvedt
Jan 09 2016 17:56 UTC
I will send my code to him if I make to the end of the tutorial :)
Elia Schito
@elia
Jan 09 2016 17:58 UTC
@saltvedt great! I'm also interested in hearing you considerations about anything that could smooth out the first approach with Opal
Torstein Saltvedt
@saltvedt
Jan 09 2016 18:02 UTC
I'll let you know if I have any ideas
Elia Schito
@elia
Jan 09 2016 18:02 UTC
Thanks!
Torstein Saltvedt
@saltvedt
Jan 09 2016 18:03 UTC
maybe this tutorial is too old for it to be worth it? Uncaught TypeError: self.$canvas(...).getContext is not a function due to @context =#{canvas}.getContext('2d')` in Grid#initialize
oh.. nevermind
I made a mistake, changed the id of the canvas element
George Plymale II
@ylluminarious
Jan 09 2016 18:33 UTC
@elia re: 0.9.1, :clap:
Brady Wied
@wied03
Jan 09 2016 20:12 UTC
@elia - Yay for 0.9.1!
Elia Schito
@elia
Jan 09 2016 23:46 UTC
@/all issuing 0.9.2 right now as 0.9.1 was built with ruby 2.3.0 which for some reason makes the gem un-installable
@/all I'm also going to yank 0.9.1