OpalRB: Using functions as parameters

170 Views Asked by At

I'm using Opal in my MeteorJS project.

I'm trying to figure out a way to do the Meteor.startup code, but it doesn't seem to work..

I thought this would work:

require 'native'

@Meteor = `Meteor`
@Meteor.startup do
  puts 'Go'
end

But it doesn't. The compiled code should be like the following:

Meteor.startup( function() {
  console.log( "GO" );
} );

It's very regular to throw functions as parameters in JS, how would we do this in Opal?

2

There are 2 best solutions below

0
On BEST ANSWER

The following should work fine:

require 'native'

@Meteor = Native(`Meteor`)
@Meteor.startup -> {
  puts 'Go'
}

Note that using Native you pass a lambda instead of a block

0
On

You can either use Native (which underneath wraps JS objects), as suggested by Elia or...

@Meteor = `Meteor`
@Meteor.JS.startup do
  puts 'Go'
end

Calls like X.JS.y compile directly to X.y(). Similarly, you can access properties like X.JS[:propname] (compiled to X.propname)