npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

norby

v0.2.1

Published

Call your Ruby libraries from node.js

Downloads

7

Readme

norby

NPM version Build Status

Call your Ruby libraries from node.js

var ruby = require('norby');

var Time = ruby.getClass('Time');
var t = new Time(2014, 7, 2);
console.log('Year: ' + t.year()); // => 'Year: 2014'
console.log(t); // => '2014-07-02 00:00:00 -0400'

To install

Prerequisites:

* node.js >= 0.10
* ruby >= 1.9

Install using npm:

npm install norby

Compile from repository:

git clone https://github.com/rylanc/norby.git
cd norby
npm install

Run unit tests:

npm test

What's missing

norby is currently in an early beta state. Check back for updates as features are implemented. Feel free to add an issue for any bugs or missing features.

  • Windows support. node.js is built with Visual Studio while most Windows Ruby installers use MinGW. It may work if you build Ruby with VS, but I haven't tried it yet.
  • Support for Ruby version 1.8.X
  • Support for Ruby including/extending
  • Support for Ruby hashes
  • Conversion of JS objects (that aren't wrapped Ruby objects)
  • Support for Ruby global variables

API

ruby

Exposed by require('norby').

ruby#require(name:String)

Calls Ruby's require method with the specified name.

ruby#eval(code:String [, binding:Binding [, filename:String [, lineno:Number]]])

Calls Ruby's eval method with the specified parameters.

ruby#getClass(name:String)

Returns a wrapped Ruby class (or struct) specified by name. The new class method will be called when the constructor is called.

var Time = ruby.getClass('Time');
var t = new Time(2014, 7, 2);

To get a class within a module, separate the module and class with ::.

var ZlibInflate = ruby.getClass('Zlib::Inflate');

Class methods and constants are exposed as properties of the constructor.

var Time = ruby.getClass('Time');
var t = Time.utc(2014, 7, 2);

var File = ruby.getClass('File');
console.log(File.SEPARATOR); // => '/'

ruby#newInstance(className:String [, …])

Returns a new instance of a Ruby object specified by className. Any additional arguments will be passed on to the class's new method.

var t = ruby.newInstance('Time', 2014, 7, 2);

ruby#inherits(derived:Constructor, superName:String)

Creates a new Ruby class (named derived.name) who's superclass is specified by superName. All public instance methods of superName will be added to the derived class's prototype. To add methods to the derived class, call its defineMethod function. This will add the method to the class's prototype and override the Ruby superclass's method. Adding the method to the prototype only will fail to override the superclass's method.

# base.rb
class Base
  def call_me
  end
  def make_call
    call_me
  end
end
ruby.require('./base');

function Derived() {
  Derived.super_.apply(this, arguments);
  this.val = 'Hello';
}
ruby.inherits(Derived, 'Base');

Derived.defineMethod('call_me', function() {
  console.log('In JS: ' + this.val);
});

var d = new Derived();
d.make_call(); // => 'In JS: Hello'

ruby#getMethod(name:String)

Returns a JS function that wraps the Ruby method specified by name. This currently only works with Kernel methods.

# hello.rb
def my_func (name)
  puts "Hello, #{name}!"
end
ruby.require('./hello');
var my_func = ruby.getMethod('my_func');
my_func('Stan');

ruby#getConstant(name:String)

Returns the Ruby constant specified by name. To get a constant within a module or class, separate the module and constant with ::. Without the separator, it returns an Object constant. getConstant can also be used to return modules.

# const.rb
module MyMod
  MY_CONST = "abcde"
end

class MyClass
  OTHER_CONST = "fghi"
end
ruby.require('./const');
var RUBY_VERSION = ruby.getConstant('RUBY_VERSION');
console.log(ruby.getConstant('MyMod::MY_CONST')); // => 'abcde'
console.log(ruby.getConstant('MyClass::OTHER_CONST')); // => 'fghi'

Ruby objects

Methods

Wrapped Ruby objects expose their public instance methods through function properties.

var Time = ruby.getClass('Time');
var t = new Time(2014, 7, 2);
console.log(t.year()); // => '2014'

The Ruby to_s method is mapped to the JS toString() function.

console.log(t.toString()); // => '2014-07-02 00:00:00 -0400'

Since node's console.log() function calls inspect with a depth argument, it is ignored when passed to the Ruby inspect method.

console.log(t); // => '2014-07-02 00:00:00 -0400'

Blocks

If the last argument in a method call is a function, it is passed to the method as a Ruby block.

var Regexp = ruby.getClass('Regexp');
var pat = new Regexp('.at');
pat.match('cat', function() {
  console.log('match!');
}); // => 'match!'

Ruby Modules

To retrieve Ruby modules, call ruby#getConstant. Modules are returned as JS objects with their class methods and constants as properties.

# mod.rb

module MyMod
  MY_CONST = "abcde"
  
  def MyMod.say_hi
    puts "Hello"
  end
end
ruby.require('./mod');
var MyMod = ruby.getConstant('MyMod');

console.log(MyMod.MY_CONST); // => 'abcde'
MyMod.say_hi(); // => 'Hello'

Type conversion

From node

null and undefined

Are converted to nil.

booleans

Are converted to Ruby booleans.

numbers

If the number can be determined to be an integer (v8::Value::IsInt32()), it's converted to a Ruby Fixnum. Otherwise, it's converted to a Float.

arrays

Are converted to Ruby arrays. Their contents are recursively converted.

strings

Are converted to Ruby strings.

Ruby objects

Wrapped Ruby objects are unwrapped.

From Ruby

nil

Is converted to undefined.

booleans

Are converted to JS booleans.

Floats

Are converted to JS numbers.

Fixnums

Are converted to JS numbers. Keep in mind that JS stores numbers as double precision floating point numbers, meaning that Fixnums (and Bignums) will lose precision above 253.

Bignums

Are converted to JS numbers if they are less than or equal to Number.MAX_VALUE. Otherwise, they are converted into JS strings. See the above note about precision.

arrays

Are converted to JS arrays. Their contents are recursively converted.

strings

Are converted to JS strings.

Symbols

Are converted to JS strings.

Ruby objects

Are wrapped as JS objects.

Ruby exceptions

Are caught within norby native code, converted to v8 exceptions and rethrown. The thrown v8 exceptions have a rubyStack property that holds the result of calling Exception#backtrace on the original Ruby exceptions.

License

The MIT License (MIT)

Copyright (c) 2014 Rylan Collins

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.