Developing cross platform desktop application with Ruby
-
Upload
anis-ahmad -
Category
Technology
-
view
12.131 -
download
1
description
Transcript of Developing cross platform desktop application with Ruby
Desktop Application with Ruby
Getting started with
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.
!=
Ruby is not ONLY for web
It's a generic purpose language
generic purpose?
● Web Application
● Desktop Application
● Mobile Application (Yes iPhone too!)
● DSLs
● Antyhing you can think*
So, Ruby can make Desktop App?
Okay...But HOW?
Many Ways!
Shoes Ruby-GNOME2 / GTK
WxRuby Ruby-Tk
Ruby Cocoa / MacRuby
QtRuby JRuby + Swing
FxRuby JRuby + SWT
Many Ways!
Shoes Ruby-GNOME2 / GTK
WxRuby Ruby-Tk
Ruby Cocoa / MacRuby
QtRuby JRuby + Swing
FxRuby JRuby + SWT
Shoes
cross-platform toolkit for writing graphical apps easily
and artfully using Ruby
Starting with Shoes
● Download (http://shoesrb.com/downloads))
● Run Shoes
Starting with Shoes
● Download (http://shoesrb.com/downloads))
● Run Shoes
Shoes example
Shoes.app :width => 300, :height => 200 do button("Click me!") { alert("Good job.") }end
Shoes example
Shoes.app :width => 300, :height => 200 do button("Click me!") { alert("Good job.") }end
Shoes example (Clock)
Shoes.app do @time = title "0:00" every 1 do @time.replace(Time.now.strftime("%I:%M %p")) endend
Packaging Shoes
JRuby
The Ruby Programming Language on the JVM
Ruby for desktop application? Yes we can!
Why JRuby?
● High performance
Why JRuby?
● High performance
● Real threading
Why JRuby?
● High performance
● Real threading
● Vast array of libraries (gems + JARs)
Why JRuby?
● High performance
● Real threading
● Vast array of libraries (gems + JARs)
● Platform Independent
Why JRuby?
● High performance
● Real threading
● Vast array of libraries (gems + JARs)
● Platform Independent
● Enterprise Acceptance
Why JRuby?
● High performance
● Real threading
● Vast array of libraries (gems + JARs)
● Platform Independent
● Enterprise Acceptance
The best of both worlds!
Let's make with JRuby!
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
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
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
Let's run it!
$ cd go/to/source/dir$ jruby number_converter.rb
Place container
super('Number Format Converter')
main = JPanel.new;
get_content_pane().add("Center", main);
Set Layout
super('Number Format Converter')
main = JPanel.new;main.set_layout(GridLayout.new(3,3,2,2))
get_content_pane().add("Center", main)
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))
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
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"));
Let's run again!
$ cd go/to/source/dir$ jruby number_converter.rb
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 }
Set Event Handler
main.add(btn = Jbutton.new("CONVERT"));btn.add_action_listener do |evt|
result.set_text(convert.upcase);end
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
Java <=> JRuby Transformation
Java <=> JRuby Transformation
new Car(color, wheels)
Car.new color, wheels
Java <=> JRuby Transformation
JLabel.LEFT
JLabel::LEFT
Java <=> JRuby Transformation
Obj.longFuncName();
Obj.long_func_name
Java <=> JRuby Transformation
String[] options = {"all", "any"};
combo = new JcomboBox(options);
options.each{ |format|
combo.add_item format
}
Java <=> JRuby Transformation
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
doSomething();
}
});
btn.add_action_listener do |evt|
do_something
end
Deployment
Warbler
https://github.com/jruby/warblerhttp://rawr.rubyforge.org/
BTW, JRuby is not ONLY for Desktop
● It's just Ruby*● Rails just works
*Here are differences- https://github.com/jruby/jruby/wiki/DifferencesBetweenMriAndJruby
JRuby Frameworks
● Cheri::Swing
● Limelight
● Monkeybars
● RSwing
● Rubeus
● Swiby
https://github.com/jruby/jruby/wiki/GUIFrameworks
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
Questions?