54
Desktop Application with Ruby Getting started with

Developing cross platform desktop application with Ruby

Embed Size (px)

DESCRIPTION

A brief introduction and example of developing desktop application with Ruby programming language. JRuby and shoesrb is discussed as platform. Prepared for and Presented on Ruby Conference Bangladesh 2003.

Citation preview

Page 1: Developing cross platform desktop application with Ruby

Desktop Application with Ruby

Getting started with

Page 2: Developing cross platform desktop application with Ruby

I am...

PHP ZEND KOHANA JSE JEE Ruby GWT JavaScript

jQuery sinatra BackboneJS CSS3 HTML5 MySQL Drupal MongoDB PHPUNIT Groovy

JRuby Symfony2 SWING

sqlite Doctrain solr Phing grails ...

Anis Uddin AhmadCTO & Co-FounderWNeeds Ltd.

Page 3: Developing cross platform desktop application with Ruby

!=

Page 4: Developing cross platform desktop application with Ruby

Ruby is not ONLY for web

Page 5: Developing cross platform desktop application with Ruby

It's a generic purpose language

Page 6: Developing cross platform desktop application with Ruby

generic purpose?

● Web Application

● Desktop Application

● Mobile Application (Yes iPhone too!)

● DSLs

● Antyhing you can think*

Page 7: Developing cross platform desktop application with Ruby

So, Ruby can make Desktop App?

Okay...But HOW?

Page 8: Developing cross platform desktop application with Ruby

Many Ways!

Shoes Ruby-GNOME2 / GTK

WxRuby Ruby-Tk

Ruby Cocoa / MacRuby

QtRuby JRuby + Swing

FxRuby JRuby + SWT

Page 9: Developing cross platform desktop application with Ruby

Many Ways!

Shoes Ruby-GNOME2 / GTK

WxRuby Ruby-Tk

Ruby Cocoa / MacRuby

QtRuby JRuby + Swing

FxRuby JRuby + SWT

Page 10: Developing cross platform desktop application with Ruby

Shoes

cross-platform toolkit for writing graphical apps easily

and artfully using Ruby

Page 11: Developing cross platform desktop application with Ruby

Starting with Shoes

● Download (http://shoesrb.com/downloads))

● Run Shoes

Page 12: Developing cross platform desktop application with Ruby

Starting with Shoes

● Download (http://shoesrb.com/downloads))

● Run Shoes

Page 13: Developing cross platform desktop application with Ruby

Shoes example

Shoes.app :width => 300, :height => 200 do  button("Click me!") { alert("Good job.") }end

Page 14: Developing cross platform desktop application with Ruby

Shoes example

Shoes.app :width => 300, :height => 200 do  button("Click me!") { alert("Good job.") }end

Page 15: Developing cross platform desktop application with Ruby

Shoes example (Clock)

Shoes.app do  @time = title "0:00"  every 1 do    @time.replace(Time.now.strftime("%I:%M %p"))  endend

Page 16: Developing cross platform desktop application with Ruby

Hackety Hack!

http://hackety.com/

Page 17: Developing cross platform desktop application with Ruby

Packaging Shoes

Page 18: Developing cross platform desktop application with Ruby

JRuby

The Ruby Programming Language on the JVM

Page 20: Developing cross platform desktop application with Ruby

The Redcar Editor

http://redcareditor.com/

Page 21: Developing cross platform desktop application with Ruby

Why JRuby?

● High performance

Page 22: Developing cross platform desktop application with Ruby

Why JRuby?

● High performance

● Real threading

Page 23: Developing cross platform desktop application with Ruby

Why JRuby?

● High performance

● Real threading

● Vast array of libraries (gems + JARs)

Page 24: Developing cross platform desktop application with Ruby

Why JRuby?

● High performance

● Real threading

● Vast array of libraries (gems + JARs)

● Platform Independent

Page 25: Developing cross platform desktop application with Ruby

Why JRuby?

● High performance

● Real threading

● Vast array of libraries (gems + JARs)

● Platform Independent

● Enterprise Acceptance

Page 26: Developing cross platform desktop application with Ruby

Why JRuby?

● High performance

● Real threading

● Vast array of libraries (gems + JARs)

● Platform Independent

● Enterprise Acceptance

The best of both worlds!

Page 27: Developing cross platform desktop application with Ruby

Let's make with JRuby!

Page 28: Developing cross platform desktop application with Ruby

Get JRuby

● Download

● Extract

● Add bin subdirectory to your $PATH

– (PATH=path/to/jruby/bin:$PATH)

● Test it: jruby -v

● Assuming you have jdk 1.7 installed● Can be installed with rvm too

Page 29: Developing cross platform desktop application with Ruby

Import Java Swing in .rb

include Java

import javax.swing.JFrameimport javax.swing.JComboBoximport javax.swing.JButtonimport javax.swing.JPanelimport javax.swing.JLabelimport javax.swing.JTextField

import java.awt.GridLayout

Page 30: Developing cross platform desktop application with Ruby

Make a Frame (JFrame)

class NumberConverter < JFrame

def initializesuper('Number Format Converter')

set_size(400,140);set_visible(true);

       set_default_close_operation(JFrame::EXIT_ON_CLOSE);end

end

num_converter = NumberConverter.new

Page 31: Developing cross platform desktop application with Ruby

Let's run it!

$ cd go/to/source/dir$ jruby number_converter.rb

Page 32: Developing cross platform desktop application with Ruby

Place container

super('Number Format Converter')

main = JPanel.new;

get_content_pane().add("Center", main);

Page 33: Developing cross platform desktop application with Ruby

Set Layout

super('Number Format Converter')

main = JPanel.new;main.set_layout(GridLayout.new(3,3,2,2))

get_content_pane().add("Center", main)

Page 34: Developing cross platform desktop application with Ruby

Add Components

main.set_layout(GridLayout.new(3,3,2,2))

main.add(JLabel.new("Convert From : ", JLabel::RIGHT))main.add(@cmbFrom = JComboBox.new)main.add(@input = JTextField.new(15))

Page 35: Developing cross platform desktop application with Ruby

Where are they going?

main.set_layout(GridLayout.new(3,3,2,2))

main.add(JLabel.new("Convert From : ", JLabel::RIGHT))main.add(@cmbFrom = JComboBox.new)main.add(@input = JTextField.new(15))

1 2 3

4 5 6

7 8 9

Page 36: Developing cross platform desktop application with Ruby

Add the rest of

main.add(@input = JTextField.new(15))

# Second rowmain.add(JLabel.new("Convert To : ", JLabel::RIGHT));main.add(@cmbTo = JComboBox.new);main.add(result = JTextField.new(15));

# Third rowmain.add(JLabel.new(" ")); # Leave this cell blankmain.add(btn = JButton.new("CONVERT"));

Page 37: Developing cross platform desktop application with Ruby

Let's run again!

$ cd go/to/source/dir$ jruby number_converter.rb

Page 38: Developing cross platform desktop application with Ruby

More with components

main.add(result = JTextField.new(15));result.set_editable(false);

main.add(btn = Jbutton.new("CONVERT"));

# Add options to comboBoxesnum_formats = ["Decimal", "Binary", "HexaDecimal", "Octal"]num_formats.each{|format| @cmbFrom.add_item format }num_formats.each{|format| @cmbTo.add_item format }

Page 39: Developing cross platform desktop application with Ruby

Set Event Handler

main.add(btn = Jbutton.new("CONVERT"));btn.add_action_listener do |evt|

result.set_text(convert.upcase);end

Page 40: Developing cross platform desktop application with Ruby

Set Event Handler cont.

def convert# Take the value of @input# Take formats form @cmbFrom and @cmbTo# Convert to required format

end

end # end of class NumFormat

main.add(btn = Jbutton.new("CONVERT"));btn.add_action_listener do |evt|

result.set_text(convert.upcase);end

Page 41: Developing cross platform desktop application with Ruby

Yep! It's Done!!

https://github.com/ajaxray/jruby-swing

Page 42: Developing cross platform desktop application with Ruby
Page 43: Developing cross platform desktop application with Ruby

Java <=> JRuby Transformation

Page 44: Developing cross platform desktop application with Ruby

Java <=> JRuby Transformation

new Car(color, wheels)

Car.new color, wheels

Page 45: Developing cross platform desktop application with Ruby

Java <=> JRuby Transformation

JLabel.LEFT

JLabel::LEFT

Page 46: Developing cross platform desktop application with Ruby

Java <=> JRuby Transformation

Obj.longFuncName();

Obj.long_func_name

Page 47: Developing cross platform desktop application with Ruby

Java <=> JRuby Transformation

String[] options = {"all", "any"};

combo = new JcomboBox(options);

options.each{ |format|

combo.add_item format

}

Page 48: Developing cross platform desktop application with Ruby

Java <=> JRuby Transformation

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

doSomething();

}

});

btn.add_action_listener do |evt|

do_something

end

Page 49: Developing cross platform desktop application with Ruby

Deployment

Warbler

https://github.com/jruby/warblerhttp://rawr.rubyforge.org/

Page 50: Developing cross platform desktop application with Ruby

BTW, JRuby is not ONLY for Desktop

● It's just Ruby*● Rails just works

*Here are differences- https://github.com/jruby/jruby/wiki/DifferencesBetweenMriAndJruby

Page 51: Developing cross platform desktop application with Ruby

JRuby Frameworks

● Cheri::Swing

● Limelight

● Monkeybars

● RSwing

● Rubeus

● Swiby

https://github.com/jruby/jruby/wiki/GUIFrameworks

Page 52: Developing cross platform desktop application with Ruby

JRuby Frameworks

● Cheri::Swing

● Limelight

● Monkeybars

● RSwing

● Rubeus

● Swiby

Frame.new("hello, world") do |frame| frame.default_close_operation = :exit_on_close frame.size = [200, 200] ...

https://github.com/jruby/jruby/wiki/GUIFrameworks

Page 53: Developing cross platform desktop application with Ruby

Questions?

Page 54: Developing cross platform desktop application with Ruby