Skip to content

DubyFeaturesOnTheJVM

vito edited this page Sep 13, 2010 · 5 revisions

One of Duby’s supported backends is for the JVM, either by outputting JVM bytecode or by outputting .java source files. This page lists the supported features from Ruby or Java that are currently working in Duby’s JVM backend.

Builtin functions

puts and print

Like Ruby, “puts” writes its argument to stdout followed by a newline and print does the same without a newline. Each call translates to the equivalent System.out PrintStream “print” or “println” call.

puts "hello"
puts "goodbye"

Method definition

In general, Duby’s method definition syntax looks like Ruby.

Argument type declarations

Method arguments are declared using a colon, with the identifier on the left and the type on the right

def foo(a:int)
  puts a
end
foo(42) # prints "42"

Return type and “throws”

The return type of a method and any exceptions it throws should be specified with “returns” and “throws” calls at the beginning of the method.

def foo(a:String)
  returns void
  throws NullPointerException
end

Arguments can be specific symbolic type names or the special “dynamic” name which indicates a runtime-bound type. You can use a mix of static and dynamic types.

def foo(a:String, b:dynamic)
  puts a
  puts b.get(0)
end
foo('hello', [1,2,3]) # prints "hello" and "1"

Toplevel methods

Methods defined at the toplevel of a script become static methods on the script’s toplevel class.

~/projects/duby ➔ bin/dubyc -e "def foo; end"
~/projects/duby ➔ javap DashE
Compiled from DashE
public class DashE extends java.lang.Object{
    public static void main(java.lang.String[]);
    public static void foo();
    public DashE();
}

Note also that the body of the script, if any is present, will become the “main” method for the script’s toplevel class.

Instance methods

Normal methods defined within class bodies are defined as instance methods.

class Foo
  def bar
    puts "I am an instance method"
  end
end
Foo.new.bar # => "I am an instance method"

Static methods

Static methods are defined like class methods or “self methods” in Ruby.

class Foo
  def self.bar
    puts "I am a static method"
  end
end
Foo.bar # => "I am a static method"

Constructors

Constructors are defined as in Ruby, with the special “initialize” method.

class Foo
  def initialize
    puts "inside constructor"
  end
end
Foo.new # => "inside constructor"

Overloading

Like Java but unlike Ruby, Duby supports overloading on arity and type of arguments.

Optional arguments

Duby supports specifying defaults for trailing arguments.

def foo(a, b = 1, c = 'hello')
  puts "a = #{a}, b = #{b}, c = #{c}"
end
foo(0) # => "a = 0, b = 1, c = hello"
foo(0, 1, 2 # => "a = 0, b = 1, c = 2"
foo # => compile error

At a JVM level, this is accomplished by chaining together overloaded methods of increasing arity, filling in missing args each step of the way. The defaults can be arbitrarily complex, but it is recommended to keep them simple literals or expressions. If you need more complicated default arguments, it may be better to define the overloads yourself.