Skip to content

Instantly share code, notes, and snippets.

@keleshev
Last active March 30, 2016 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keleshev/93064a400ff25961212231667cdd80c8 to your computer and use it in GitHub Desktop.
Save keleshev/93064a400ff25961212231667cdd80c8 to your computer and use it in GitHub Desktop.

Rosetta stone: Quark → Ruby

Example

Quark

package MyPackage 1.0.0;

namespace my_namespace {
  interface MyInterface {
    String hello();
  }

  class MyClass extends MySuperClass, MyInterface {
    String my_field;

    MyClass(String value) {
      self.my_field = value;
    }
  }
}

Ruby

module MyPackage
  module MyNamespace
    # interface MyInterfaced is erased

    class MyClass < MySuperClass
      attr_accessor :my_field

      def initialize(value)
        @my_field = value
      end
    end
  end

  # Case-preserving alias
  def self.my_namespace
    MyNamespace
  end
end

Package

Each .q file is a Quark package. It's name and version is declared as:

package MyPackage 1.0.0;

Quark package maps to a Ruby gem with a top-level module of same name as the package.

require "MyPackage"

p MyPackage # => MyPackage

Interface

Quark interfaces are erased before compiling to Ruby.

Class

Quark classes are mapped straightforwardly to Ruby classes

class MyClass extends MySuperClass, MyInterface {
  String my_field;

  MyClass(String value) {
    self.my_field = value;
  }
}
class MyClass < MySuperClass
  attr_accessor :my_field

  def initialize(value)
    @my_field = value
  end
end

Aliases

Ruby requires classes, methods, and constants to be capitalized, while Quark does not. If Quark name is capitalized, it is preserved. If Quark name is not-capitalized, like my_namespace, then it is converted to a capitalized name, like MyNamespace plus, there is an alias method generated that preserves the original case.

namespace my_namespace {
  // ...
}
module MyNamespace
  # ...
end

def self.my_namespace
  MyNamespace
end

Import

import my_package;
require "my_package"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment