Jay Ruby

JRuby is a RubyLanguage interpreter written in JavaLanguage for running on the JavaVirtualMachine

It allows access to the Java classes and libraries when programming in Ruby. You can also invoke the JRuby interpreter from Java.

JRuby 1.0 was released June 12, 2007. It is Ruby version 1.8.5-compatible and provides the vast majority of the usual Ruby standard library. It also comes with RubyGems and RakeMake.


JRuby with JavaSwing

 #!/usr/bin/env jruby

# File hello_world_swing.rb (2007-08-13) # A translation of Java tutorial HelloWorldSwing.java into JRuby 1.0 # http://java.sun.com/docs/books/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java

require 'java' include_class 'javax.swing.JFrame' include_class 'javax.swing.JLabel' include_class 'javax.swing.SwingUtilities'

def create_and_show_gui() # Create and set up the window frame = JFrame.new("Swingin' JRuby") frame.set_default_close_operation(JFrame::EXIT_ON_CLOSE)

# Add the ubiquitous Hello World label label = JLabel.new("Hello, JRuby 1.0. How's it swingin'?") frame.get_content_pane.add(label)

# Display the window frame.pack frame.set_visible(true) end

if $0 == __FILE__ class HelloRunnable # Implement interface by including module include java.lang.Runnable

def run() create_and_show_gui end end

# Schedule a job for the event-dispatching thread # creating and showing this application's GUI SwingUtilities.invoke_later(HelloRunnable.new) end

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#!/usr/bin/env jruby

# File file_system_lister.rb (2007-08-13) # Use CLI or JFileChooser to select a directory for file system listing # Don't bother loading the Java libraries if we don't need them

require 'find'

def list_file_sizes(dir_path) puts format('%10s : %s', 'BYTES', 'FILE PATH') Find.find(dir_path) do |f| puts format('%10d : %s', FileTest.size(f), f) if FileTest.file?(f) end end

dir_path = ARGV[0]

if dir_path.nil? require 'java' include_class 'javax.swing.JFileChooser'

chooser = JFileChooser.new # starts at home directory by default chooser.set_file_selection_mode(JFileChooser::DIRECTORIES_ONLY) return_val = chooser.show_open_dialog(javax.swing.JPanel.new)

if return_val == JFileChooser::APPROVE_OPTION dir_path = chooser.get_selected_file.get_path end end

list_file_sizes(dir_path) unless dir_path.nil? exit # important


RubyLanguage and JavaLanguage DataStructures

 # Ruby Array <-> Java array
 jArray = rArray.to_java
 rArray = jArray.to_a    # or Array.new(jArray)

# Ruby Array <-> Java Vector jVector = Vector.new rArray rArray = jVector.to_a # Array.new(jVector) won't work

# Ruby Array of Arrays <-> Java Vector of Vectors jVoV = rAoA.inject(Vector.new) { |v, row| v.addElement(Vector.new(row)) ; v } rAoA = jVoV.inject([]) { |a, row| a << row.to_a }

# Ruby Array <-> Java ArrayList jArrayList = ArrayList.new rArray rArray = jArrayList.to_a

# Ruby Array <-> Java LinkedList jLinkedList = LinkedList.new rArray rArray = jLinkedList.to_a

# Ruby Set <-> Java HashSet jHashSet = HashSet.new rSet.to_a rSet = Set.new jHashSet

# Ruby SortedSet <-> Java TreeSet jTreeSet = TreeSet.new rSortedSet.to_a rSortedSet = SortedSet.new jTreeSet

# Ruby Hash <-> Java Hashtable (caution, this will remove any nesting) jHashtable = Hashtable.new rHash rHash = Hash[*jHashtable.to_a.flatten]

# Ruby Hash <-> Java HashMap (caution, this will remove any nesting) jHashMap = HashMap.new rHash rHash = Hash[*jHashMap.to_a.flatten]

# Ruby Hash <-> Java TreeMap jTreeMap = TreeMap.new rHash rHash = Hash[*jTreeMap.to_a.flatten]

I have yet to figure out how to construct an array of arrays which a Java method accepts as an Object[][] argument. So I look around for an overloaded method which accepts a Vector of Vectors. Does anyone know if JRuby can cobble together something which passes as Object[][]? -- ElizabethWiethoff
 # Generate Object[][] from 2D ruby array:
 [['a', 'b'], ['d', 'e']].to_java(java.lang.Object[])


CategoryRuby, CategoryJava


EditText of this page (last edited October 1, 2014) or FindPage with title or text search