Skip to content
xraid edited this page Sep 13, 2010 · 29 revisions

To run a Duby file :

$ bin/duby examples/fields.duby # would run the duby code trough the JVM live

To create a .java source file from a .duby source file :

$ bin/dubyc -java examples/fields.duby # would create a java source file i.e. fields.java

To compile to a .class bytecode file from a .duby source file :

$ bin/dubyc examples/fields.duby # would compile to a java class file i.e. fields.class

To print out a duby file as AST with transformation applied :

$ bin/dubyp examples/fields.duby # would print out the AST with transformation applied 

Fibonacci examples/fib.duby

# rename fib.duby to Fib.duby to run ... since the method is named fib as well and the class is not defined other then by the 
# name of the .duby file in this example ... there is a class fib with method fib but the code only calls fib the class not fib.fib() the class.method() ... 
# this is an issue in JIRA about to resolve ...

Identical to Ruby, except for the argument type declaration

def fib(a:int)
   if a < 2
     a
   else
     fib(a - 1) + fib(a - 2)
   end
 end

Fractal benchmark examples/fractal.duby

Also identical to Ruby, other than the argument type declaration in iterate.

import java.lang.System
 
def run
   puts "Rendering"
   y = -39.0
   while y <= 39.0
     puts
     x = -39.0
     while x <= 39.0
       i = iterate(x/40.0, y/40.0)
       if (i == 0)
         print "*"
       else
         print " "
       end
       x += 1
     end
     y += 1
   end
   puts
 end

def iterate(x:double, y:double)
   cr = y-0.5
   ci = x
   zi = 0.0
   zr = 0.0
   i = 0
   result = 0
   while true
      i += 1
      temp = zr * zi
      zr2 = zr * zr
      zi2 = zi * zi
      zr = zr2 - zi2 + cr
      zi = temp + temp + ci
      if (zi2 + zr2 > 16)
        result = i
        break
      end
      if (i > 1000)
        result = 0
        break
      end
   end
   result
 end
i = 0
while i < 10
   start = System.currentTimeMillis
   run
   puts "Time: " + (System.currentTimeMillis - start) / 1000.0
   i += 1
end

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

Also check out the examples/appengine code that runs Duby on appengine with the appengine db.

http://duby-app.appspot.com/

http://code.google.com/p/appengine-jruby/

Clone this wiki locally