Last Updated: February 25, 2016
·
451
· wstucco

Opal (the Ruby to Javascript compiler) and Meteor

If you're a rubyst, you probably know Opal already.

For those who don't

Opal is a ruby to javascript compiler, an implementation of the ruby corelib and stdlib, and associated gems for building fast client side web applications in ruby.
http://opalrb.org/docs/getting_started/

On the other side, Meteor is

an ultra-simple, database-everywhere, data-on-the-wire, pure-Javascript web framework.

Which basically means Meteor is a reactive framework, built on node and javascript, it is isomorphic (a.k.a it enables the developer to write the same code on the client and the server) and automatically synchronise changes across clients and between clients and server.

I was working on a Meteor project and was unsatisfied of Javascript, so I tried Coffeescript, but Meteor still does not integrate perfectly with it, so I started thinking if there was a way I could use Ruby through Opal.

A weekend later I published an Opal build plugin for Meteor, it's very basic, but it works.

On the Atmosphere page you can find some preliminary documentation on how to use it.

I've also published a Gist with a basic Opal-Meteor example on how to wrap Meteor builtin objects into Opal classes.

This is the code in all its glory

# user.rb
class User
  attr_accessor :name

  def initialize(name)
    @name = name
  end

  def admin?
    @name == 'Admin'
  end
end
# meteor.rb
# Meteor Objects wrapper
class Meteor
  def self.server?
    `Meteor.isServer`
  end

  def self.client?
    `Meteor.isClient`
  end

  def self.startup(&block)
    `#{block.call if block_given?}`
  end

end
# main.rb
user = User.new('Admin')
puts user
puts user.admin?

puts "hello from client #{user.name}!" if Meteor.client?
puts "hello from server #{user.name}!" if Meteor.server?

I encourage you to put the plugin under test and signal me bugs or desiderata.