Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created December 22, 2013 00:28
Show Gist options
  • Save JoshCheek/8077006 to your computer and use it in GitHub Desktop.
Save JoshCheek/8077006 to your computer and use it in GitHub Desktop.
Helper method for looking at how Opal turns Ruby code into JS. Runs anywhere coderay is loaded, e.g. pry and `irb -r coderay`
# try options like arity_cheeck: true
def h(opal_code, options={})
javascript = Opal.compile opal_code, options
puts CodeRay.highlight(javascript, :javascript, {}, :terminal)
end
@JoshCheek
Copy link
Author

Example:

> h "def add(a, b) a + b end; add 1, 2"
/* Generated by Opal 0.5.5 */
(function($opal) {
  var self = $opal.top, $scope = $opal, nil = $opal.nil, $breaker = $opal.breaker, $slice = $opal.slice;
  $opal.add_stubs(['$+', '$add']);
  $opal.Object._proto.$add = function(a, b) {
    var self = this;
    return a['$+'](b);
  };
  return self.$add(1, 2);
})(Opal);
=> nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment