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.

How to compile hello-world

In this post you will see how the InfraRuby statically typed Ruby compiler translates hello-world to Java bytecode.

Consider this program:

puts "hello, world"

The InfraRuby compiler translates this program into three Java class files:

ruby.main

InfraRuby defines an instance method main in class ruby.main with four instructions: load STDOUT, load the String constant, call puts, and return:

  public void main()
       0: invokestatic  ruby/IO$eigen.STDOUT:()Lruby/IO;
       3: getstatic     ruby/main$const.x0:Lruby/String;
       6: invokevirtual ruby/IO.puts:(Lruby/String;)V
       9: return

ruby.main$const

InfraRuby defines a static initializer in class ruby.main$const to create the String constant:

  static {}
       0: ldc           "hello, world"
       2: invokestatic  ruby/InfraRuby$Support$eigen.load_string_utf_8:(Ljava/lang/String;)Lruby/String;
       5: putstatic     x0:Lruby/String;
       8: return

ruby.main$eigen

InfraRuby defines a static method main in class ruby.main$eigen:

  public static void main(java.lang.String[])
       0: aload_0
       1: invokestatic  ruby/InfraRuby$Main$eigen.init:([Ljava/lang/String;)V
       4: new           ruby/main
       7: dup
       8: invokespecial ruby/main."<init>":()V
      11: invokevirtual ruby/main.main:()V
      14: aconst_null
      15: astore_1
      16: invokestatic  ruby/InfraRuby$Main$eigen.fini:()V
      19: goto          28
      22: astore_1
      23: invokestatic  ruby/InfraRuby$Main$eigen.fini:()V
      26: aload_1
      27: athrow
      28: return

Note: this method has a handler at 22 for any exception raised in 4...14.

InfraRuby compiles main programs as class ruby.main, but you can define a static method main explicitly in any class:

## <>
class MyMain
	## -> void
	def main
		puts "hello, world"
		return
	end

	class << self
		## java.lang.String[] -> void
		def main(args)
			InfraRuby::Main.init(args)
			begin
				MyMain.new.main
				return
			ensure
				InfraRuby::Main.fini
			end
		end
	end
end

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