-
Notifications
You must be signed in to change notification settings - Fork 10
DubySyntax
The Duby syntax is inspired by the Ruby language and uses the JRuby syntax with types declared.
Duby could be seen as a “Ruby with static types”, though Duby is not Ruby. It does not share Ruby’s type system, core classes, standard library, or even execution model, since none of these things are imposed upon Duby programs by the Duby language.
Below the Java import and the explicit typing in the method definition
import java.lang.System def hello(a:string)
is the only difference from a Ruby syntax, and the typing (a:string)
def hello(a:string)
is the only difference from the JRuby syntax. So Duby’s syntax is like JRuby’s syntax with types declared.
By default Duby creates a class with the same name as your file.
# --------------------------- start file code.duby
# --------------------------- start of generated Foo.java or Foo.class
import java.lang.System
class Foo
def initialize
puts "constructor"
@hello = "Hello, "
end
def hello(a:string)
puts @hello; puts a
puts System.getProperty "java.home"
end
end
# --------------------------- end of generated Foo.java or Foo.class
# --------------------------- start of generated code.java or code.class
# --------------------------- containing public static void main(String[])
Foo.new.hello("Duby")
# --------------------------- end of generated code.java or code.class
# --------------------------- end file code.duby
The above file named code.duby will compile to 2 .java or .class: Foo and code (containing public static void main(String[]).
Simple Swing application examples/swing.duby
Identical to Ruby (JRuby) other than “implements” instead of “include” for the interface impl and the use of a JButton cast in the actionPerformed implementation.
import javax.swing.JFrame
import javax.swing.JButton
import java.awt.event.ActionListener
frame = JFrame.new "Welcome to Duby"
frame.setSize 300, 300
frame.setVisible true
button = JButton.new "Press me"
frame.add button
frame.show
class AL; implements ActionListener
def initialize; end
def actionPerformed(event)
JButton(event.getSource).setText "Duby Rocks!"
end
end
button.addActionListener AL.new