InfraRuby Vision delivers solutions for mobile and web applications.
You can try InfraRuby live in your browser!
Try the InfraRuby statically typed Ruby compiler live in your browser. Try our examples or write your own code.
Follow @InfraRuby on Twitter for news and updates.

Why InfraRuby?

InfraRuby is a compiler and runtime for statically typed Ruby. In this post you will see the advantages of using InfraRuby.

compiler compatibility

The Ruby language features supported by the InfraRuby compiler have the same syntax and semantics as Ruby interpreters.

For example, this is FizzBuzz in InfraRuby:

1.upto(100) do |n|
	a = []
	a << "Fizz" if n % 3 == 0
	a << "Buzz" if n % 5 == 0
	if a.empty?
		puts n
	else
		puts a.join
	end
end

This code runs in Ruby interpreters without modification!

runtime compatibility

The Ruby language features supported by the InfraRuby runtime have the same behavior as Ruby interpreters.

The runtime implements selected methods from core classes Array Class Comparable Dir Encoding Enumerable Enumerator Exception Fiber File Float Hash Integer IO Kernel MatchData Math Mutex Numeric Object Process Random Range Regexp String Symbol Thread and standard library packages base64 benchmark date digest json net/http openssl pathname securerandom set socket stringio thread time uri.

The specs for the InfraRuby runtime are executed against Ruby interpreters to check surprise-for-surprise compatibility:

describe "String#+" do
	it "should return concatenation of strings" do
		expect("a" + "b").to eq("ab")
	end
end

This code runs in Ruby interpreters without modification!

compatibility packages

Other features supported by the compiler or runtime are available in compatibility packages for Ruby interpreters.

For example, InfraRuby supports primitive types boolean, byte, char, int16, int32, int64, float32 and float64. You can use primitive values in Ruby interpreters with the infraruby-java gem:

require "infraruby-java"

a = 1.i32
b = 2.i32
c = a + b
puts c

This code runs in Ruby interpreters without modification!

substitute gems

InfraRuby substitute gems implement features of gems for Ruby interpreters, with the same programming interface.

For example, the tzinfo-ir gem for InfraRuby implements features of the tzinfo gem for Ruby interpreters:

require "tzinfo-ir"

z = TZInfo::Timezone.get("NZ")
t = z.now
puts t
t = z.local_to_utc(t)
puts t
t = z.utc_to_local(t)
puts t

This code runs in Ruby interpreters without modification!

project conventions

InfraRuby projects are also Ruby projects and, as Ruby projects, follow the same conventions as other Ruby projects.

Every InfraRuby project is created with a foo.gemspec file, a Gemfile, a Rakefile, and a lib/foo.rb file. You can use the same tools you would use with other Ruby projects, for example:

bundle install
bundle exec rspec
gem build foo.gemspec

rake tasks

InfraRuby defines rake tasks to use the InfraRuby runtime or Ruby interpreters.

To compile the library, type:

rake library:core:compile

To compile and execute a program with the InfraRuby runtime, type:

rake program:hello-main:core:compile
rake program:hello-main:core:execute

To run a program with a Ruby interpreter, type:

rake program:hello-main:interpret:ruby

To compile and execute the specs with the InfraRuby runtime, type:

rake spec:core:compile
rake spec:core:execute

To run the specs with a Ruby interpreter, type:

rake spec:interpret:ruby

performance

When compared to JRuby, InfraRuby has faster startup (~0 seconds vs ~5 seconds, depending on your hardware) and faster runtime (~5×, depending on your code), and also lower memory usage (~3×, depending on your code).

The InfraRuby compiler is implemented in the InfraRuby language and uses JRuby to bootstrap: the compiler source is compiled to bytecode using the compiler itself under JRuby. For comparison, the compiler source is compiled again using the compiled compiler.

This chart shows, for each runtime, the time to compile the compiler source to bytecode:

Try the InfraRuby statically typed Ruby compiler live in your browser. You can use InfraRuby for your own projects with our free download!

Follow @InfraRuby on Twitter
Copyright © 2011-2017 InfraRuby Vision Limited